|
Groovy JDK |
Method Summary | |
---|---|
Calendar
|
clearTime()
Clears the time portion of this Calendar instance; useful utility where it makes sense to compare month/day/year only portions of a Calendar. |
String
|
format(String pattern)
Shortcut for SimpleDateFormat to output a String representation of this calendar instance. |
int
|
getAt(int field)
Support the subscript operator for a Calendar. |
int
|
minus(Calendar then)
Subtract another date from this one and return the number of days of the difference. |
Calendar
|
next()
Increment a Calendar by one day. |
Calendar
|
previous()
Decrement a Calendar by one day. |
void
|
putAt(int field, int value)
Support the subscript operator for mutating a Calendar. |
void
|
set(Map updates)
Support mutating a Calendar with a Map. |
Calendar
|
updated(Map updates)
Support creating a new Date having similar properties to an existing Date (which remains unaltered) but with some fields updated according to a Map of changes. |
Method Detail |
---|
public Calendar clearTime()
public String format(String pattern)
Shortcut for SimpleDateFormat to output a String representation
of this calendar instance. This method respects the Calendar's assigned
TimeZone, whereas calling cal.time.format('HH:mm:ss')
would use the system timezone.
Note that Calendar equivalents of date.getDateString()
and variants do not exist because those methods are Locale-dependent.
Although a Calendar may be assigned a Locale, that information is
lost and therefore cannot be used to control the default date/time formats
provided by these methods. Instead, the system Locale would always be
used. The alternative is to simply call
DateFormat#getDateInstance and pass the same Locale
that was used for the Calendar.
pattern
- format pattern.public int getAt(int field)
field
- a Calendar field, e.g. MONTH.public int minus(Calendar then)
then
- another Calendar.public Calendar next()
public Calendar previous()
public void putAt(int field, int value)
import static java.util.Calendar.* def cal = Calendar.instance cal[DAY_OF_WEEK] = MONDAY cal[MONTH] = MARCH println cal.time // A Monday in March
field
- A Calendar field, e.g. MONTH.value
- The value for the given field, e.g. FEBRUARY.public void set(Map updates)
java.util.Calendar#set(int, int)
.
The keys can either be the normal fields values provided as
the first parameter to that method or one of the following Strings:
year | Calendar.YEAR |
month | Calendar.MONTH |
date | Calendar.DATE |
hourOfDay | Calendar.HOUR_OF_DAY |
minute | Calendar.MINUTE |
second | Calendar.SECOND |
import static java.util.Calendar.* def cal = Calendar.instance def m = [:] m[YEAR] = 2010 m[MONTH] = DECEMBER m[DATE] = 25 cal.set(m) println cal.time // Christmas 2010 cal.set(year:2011, month:DECEMBER, date:25) println cal.time // Christmas 2010
updates
- A Map of Calendar keys and values.public Calendar updated(Map updates)
import static java.util.Calendar.YEAR def now = Calendar.instance def nextYear = now[YEAR] + 1 def oneYearFromNow = now.updated(year: nextYear) println now.time println oneYearFromNow.time
updates
- A Map of Calendar keys and values.
|
Groovy JDK |