public class DateGroovyMethods extends DefaultGroovyMethodsSupport
Constructor and Description |
---|
DateGroovyMethods() |
Modifier and Type | Method and Description |
---|---|
static Calendar |
clearTime(Calendar self)
Clears the time portion of this Calendar instance; useful utility
where it makes sense to compare month/day/year only portions of a Calendar.
|
static Date |
clearTime(Date self)
Clears the time portion of this Date instance; useful utility where
it makes sense to compare month/day/year only portions of a Date.
|
static Date |
clearTime(Date self)
Clears the time portion of this java.sql.Date instance; useful utility
where it makes sense to compare month/day/year only portions of a Date.
|
static Calendar |
copyWith(Calendar self,
Map<Object,Integer> 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.
|
static Date |
copyWith(Date self,
Map<Object,Integer> 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.
|
static void |
downto(Calendar self,
Calendar to,
Closure closure)
Iterates from the date represented by this calendar up to the date represented
by the given calendar, inclusive, incrementing by one day each time.
|
static void |
downto(Date self,
Date to,
Closure closure)
Iterates from this date down to the given date, inclusive,
decrementing by one day each time.
|
static String |
format(Calendar self,
String pattern)
Shortcut for
SimpleDateFormat to output a String representation
of this calendar instance. |
static String |
format(Date self,
String format)
Create a String representation of this date according to the given
format pattern.
|
static String |
format(Date self,
String format,
TimeZone tz)
Create a String representation of this date according to the given
format pattern and timezone.
|
static int |
getAt(Calendar self,
int field)
Support the subscript operator for a Calendar.
|
static int |
getAt(Date self,
int field)
Support the subscript operator for a Date.
|
static String |
getDateString(Date self)
Return a string representation of the 'day' portion of this date
according to the locale-specific
DateFormat.SHORT default format. |
static String |
getDateTimeString(Date self)
Return a string representation of the date and time time portion of
this Date instance, according to the locale-specific format used by
DateFormat . |
static String |
getTimeString(Date self)
Return a string representation of the time portion of this date
according to the locale-specific
DateFormat.MEDIUM default format. |
static int |
minus(Calendar self,
Calendar then)
Subtract another date from this one and return the number of days of the difference.
|
static int |
minus(Date self,
Date then)
Subtract another Date from this one and return the number of days of the difference.
|
static Date |
minus(Date self,
int days)
Subtract a number of days from this date and returns the new date.
|
static Date |
minus(Date self,
int days)
Subtract a number of days from this date and returns the new date.
|
static Timestamp |
minus(Timestamp self,
int days)
Subtract a number of days from this Timestamp and returns the new Timestamp object.
|
static Calendar |
next(Calendar self)
Increment a Calendar by one day.
|
static Date |
next(Date self)
Increment a Date by one day.
|
static Date |
next(Date self)
Increment a java.sql.Date by one day.
|
static Date |
plus(Date self,
int days)
Add a number of days to this date and returns the new date.
|
static Date |
plus(Date self,
int days)
Add a number of days to this date and returns the new date.
|
static Timestamp |
plus(Timestamp self,
int days)
Add number of days to this Timestamp and returns the new Timestamp object.
|
static Calendar |
previous(Calendar self)
Decrement a Calendar by one day.
|
static Date |
previous(Date self)
Decrement a Date by one day.
|
static Date |
previous(Date self)
Decrement a java.sql.Date by one day.
|
static void |
putAt(Calendar self,
int field,
int value)
Support the subscript operator for mutating a Calendar.
|
static void |
putAt(Date self,
int field,
int value)
Support the subscript operator for mutating a Date.
|
static void |
set(Calendar self,
Map<Object,Integer> updates)
Support mutating a Calendar with a Map.
|
static void |
set(Date self,
Map<Object,Integer> updates)
Support mutating a Date with a Map.
|
static Calendar |
toCalendar(Date self)
Convert a Date to a Calendar.
|
static Calendar |
updated(Calendar self,
Map<Object,Integer> updates)
Legacy alias for copyWith.
|
static Date |
updated(Date self,
Map<Object,Integer> updates)
Legacy alias for copyWith.
|
static void |
upto(Calendar self,
Calendar to,
Closure closure)
Iterates from the date represented by this calendar up to the date represented
by the given calendar, inclusive, incrementing by one day each time.
|
static void |
upto(Date self,
Date to,
Closure closure)
Iterates from this date up to the given date, inclusive,
incrementing by one day each time.
|
cloneSimilarCollection, cloneSimilarMap, closeQuietly, closeWithWarning, createSimilarArray, createSimilarCollection, createSimilarCollection, createSimilarCollection, createSimilarList, createSimilarMap, createSimilarOrDefaultCollection, createSimilarSet, normaliseIndex, sameType, subListBorders, subListBorders
public static int getAt(Date self, int field)
self
- a Datefield
- a Calendar field, e.g. MONTHCalendar
public static Calendar toCalendar(Date self)
self
- a Datepublic static int getAt(Calendar self, int field)
self
- a Calendarfield
- a Calendar field, e.g. MONTHCalendar
public static void putAt(Calendar self, 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
self
- A Calendarfield
- A Calendar field, e.g. MONTHvalue
- The value for the given field, e.g. FEBRUARYCalendar.set(int, int)
public static void putAt(Date self, int field, int value)
self
- A Datefield
- A Calendar field, e.g. MONTHvalue
- The value for the given field, e.g. FEBRUARYputAt(java.util.Calendar, int, int)
,
Calendar.set(int, int)
public static void set(Calendar self, Map<Object,Integer> updates)
The map values are the normal values provided as the
second parameter to 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 |
dayOfMonth | 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
self
- A Calendarupdates
- A Map of Calendar keys and valuesCalendar.set(int, int)
,
Calendar.set(int, int, int, int, int, int)
public static Calendar updated(Calendar self, Map<Object,Integer> updates)
copyWith(java.util.Calendar, java.util.Map)
public static Calendar copyWith(Calendar self, Map<Object,Integer> updates)
Example usage:
import static java.util.Calendar.YEAR def now = Calendar.instance def nextYear = now[YEAR] + 1 def oneYearFromNow = now.copyWith(year: nextYear) println now.time println oneYearFromNow.time
self
- A Calendarupdates
- A Map of Calendar keys and valuesCalendar.set(int, int)
,
Calendar.set(int, int, int, int, int, int)
,
set(java.util.Calendar, java.util.Map)
public static void set(Date self, Map<Object,Integer> updates)
The map values are the normal values provided as the
second parameter to 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 |
dayOfMonth | Calendar.DATE |
hourOfDay | Calendar.HOUR_OF_DAY |
minute | Calendar.MINUTE |
second | Calendar.SECOND |
import static java.util.Calendar.YEAR def date = new Date() def nextYear = date[YEAR] + 1 date.set(year: nextYear) println date
self
- A Dateupdates
- A Map of Calendar keys and valuesCalendar.set(int, int)
,
set(java.util.Calendar, java.util.Map)
public static Date updated(Date self, Map<Object,Integer> updates)
copyWith(java.util.Date, java.util.Map)
public static Date copyWith(Date self, Map<Object,Integer> updates)
Example usage:
import static java.util.Calendar.YEAR def today = new Date() def nextYear = today[YEAR] + 1 def oneYearFromNow = today.copyWith(year: nextYear) println today println oneYearFromNow
self
- A Dateupdates
- A Map of Calendar keys and valuesCalendar.set(int, int)
,
set(java.util.Date, java.util.Map)
,
copyWith(java.util.Calendar, java.util.Map)
public static Date next(Date self)
self
- a Datepublic static Calendar next(Calendar self)
self
- a Calendarpublic static Calendar previous(Calendar self)
self
- a Calendarpublic static Date next(Date self)
self
- a java.sql.Datepublic static Date previous(Date self)
self
- a Datepublic static Date previous(Date self)
self
- a java.sql.Datepublic static Date plus(Date self, int days)
self
- a Datedays
- the number of days to increasepublic static Date plus(Date self, int days)
self
- a java.sql.Datedays
- the number of days to increasepublic static Timestamp plus(Timestamp self, int days)
self
- a Timestampdays
- the number of days to increasepublic static Date minus(Date self, int days)
self
- a Datedays
- the number of days to subtractpublic static Date minus(Date self, int days)
self
- a java.sql.Datedays
- the number of days to subtractpublic static Timestamp minus(Timestamp self, int days)
self
- a Timestampdays
- the number of days to subtractpublic static int minus(Calendar self, Calendar then)
Date self = Date then + (Date self - Date then)
IOW, if self is before then the result is a negative value.
self
- a Calendarthen
- another Calendarpublic static int minus(Date self, Date then)
Date self = Date then + (Date self - Date then)
IOW, if self is before then the result is a negative value.
self
- a Datethen
- another Datepublic static String format(Date self, String format)
Create a String representation of this date according to the given format pattern.
For example, if the system timezone is GMT,
new Date(0).format('MM/dd/yy')
would return the string
"01/01/70"
. See documentation for SimpleDateFormat
for format pattern use.
Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
self
- a Dateformat
- the format pattern to use according to SimpleDateFormat
SimpleDateFormat
public static String format(Date self, String format, TimeZone tz)
Create a String representation of this date according to the given format pattern and timezone.
For example:
def d = new Date(0)
def tz = TimeZone.getTimeZone('GMT')
println d.format('dd/MMM/yyyy', tz)
would return the string
"01/Jan/1970"
. See documentation for SimpleDateFormat
for format pattern use.
Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
self
- a Dateformat
- the format pattern to use according to SimpleDateFormat
tz
- the TimeZone to useSimpleDateFormat
public static String getDateString(Date self)
Return a string representation of the 'day' portion of this date
according to the locale-specific DateFormat.SHORT
default format.
For an "en_UK" system locale, this would be dd/MM/yy
.
Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
self
- a DateDateFormat.getDateInstance(int)
,
DateFormat.SHORT
public static String getTimeString(Date self)
Return a string representation of the time portion of this date
according to the locale-specific DateFormat.MEDIUM
default format.
For an "en_UK" system locale, this would be HH:MM:ss
.
Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
self
- a DateDateFormat.getTimeInstance(int)
,
DateFormat.MEDIUM
public static String getDateTimeString(Date self)
Return a string representation of the date and time time portion of
this Date instance, according to the locale-specific format used by
DateFormat
. This method uses the DateFormat.SHORT
preset for the day portion and DateFormat.MEDIUM
for the time
portion of the output string.
Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
self
- a DateDateFormat.getDateTimeInstance(int, int)
public static Date clearTime(Date self)
self
- a Datepublic static Date clearTime(Date self)
self
- a java.sql.Datepublic static Calendar clearTime(Calendar self)
self
- a Calendarpublic static String format(Calendar self, 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(int, java.util.Locale)
and pass the same Locale
that was used for the Calendar.
self
- this calendarpattern
- format patternDateFormat.setTimeZone(java.util.TimeZone)
,
DateFormat.format(java.util.Date)
,
format(java.util.Date, String)
public static void upto(Date self, Date to, Closure closure)
self
- a Dateto
- another Date to go up toclosure
- the closure to callpublic static void upto(Calendar self, Calendar to, Closure closure)
self
- a Calendarto
- another Calendar to go up toclosure
- the closure to callpublic static void downto(Date self, Date to, Closure closure)
self
- a Dateto
- another Date to go down toclosure
- the closure to callpublic static void downto(Calendar self, Calendar to, Closure closure)
self
- a Calendarto
- another Calendar to go down toclosure
- the closure to call