Overview
The Calendar
class is used to manipulate dates such as adding or subtracting days, months, or years from a given date. Calendar
is more flexible than the simpler Date
object for doing things like
- Adding X number of days to a date
- Finding the first or last day of the month
- Setting specific components of a date (like the hour or minute used with Date & Time fields)
Also, the Calendar
class can be used to create date variables to be referenced in your script.
📅 Common Calendar
Fields
Field | Meaning |
|
Days |
Calendar.MONTH |
Months (0 = Jan, 11 = Dec) |
Calendar.YEAR |
Years |
Calendar.HOUR |
Hour (12-hour clock) |
Calendar.HOUR_OF_DAY |
Hour (24-hour clock) |
Calendar.MINUTE |
Minutes |
📅 Common Calendar Methods Used to Add or Subtract
Calendar Method | What It's Used For |
|
To create a calendar variable for date manipulation using the current Date/Time |
|
To set a calendar object to a custom Date. |
|
To add or subtract a specified number of Days, Months, Years, etc. |
|
To convert a Calendar object back to a Date to so that it can be stamped into a Date field. |
|
Used to start from a specific date |
calendar.getActualMinimum(Calendar.DAY_OF_MONTH)
|
Used to get the first day of the month. This will return 1. |
calendar.getActualMaximum(Calendar.DAY_OF_MONTH) |
Used to get the last day of the month. This will return 28, 29, 30, or 31 depending on the month and year. |
Examples
The two examples below show how to use a Calendar object to add 3 days to the current date, and how to subtract 3 day from the current date.
//EXAMPLE 1 - Set a Task Due Date field's Value to be 3 Days After The Current Date
//get the current date
def cal = Calendar. getInstance()
//Add 3 days to the current date
cal.add(Calendar.DAY_OF_MONTH, 3)
//get the new Date object
def dueDate = cal.getTime()
//assign the dueDate to the taskEndDate field
currentValues["taskEndDate"] = dueDate
//EXAMPLE 2 - Same as EXAMPLE 1 above, but showing how to SUBTRACT 3 Days from the Current Date instead of ADDING 3 Days
//get the current date
def cal = Calendar. getInstance()
//Add 3 days to the current date
cal.add(Calendar.DAY_OF_MONTH, -3)
//get the new Date object
def dueDate = cal.getTime()
//assign the dueDate to the taskEndDate field
currentValues["taskEndDate"] = dueDate
The example below shows how use a calendar object to set a date field's value to the first day of the month following the initial date entry.
//Create date variable
Date theDate = currentValues["taskEndDate"] as Date
//Create calendar variable for date manipulation
Calendar calendar = Calendar.getInstance()
//Set calendar date to date variable
calendar.setTime(theDate)
//Add one month to date
calendar.add(Calendar.MONTH, 1)
//Set calendar date to the first day of the month
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
//Set date variable equal to the calendar variable
theDate = calendar.getTime()
//Set date field to the new date
currentValues["taskEndDate"] = theDate
Related Articles:
Comments
0 comments
Please sign in to leave a comment.