Class DateUtilExtensions
-
Method Summary
Modifier and TypeMethodDescriptionstatic Date
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
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
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 Calendar
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
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
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
Iterates from this date down to the given date, inclusive, decrementing by one day each time.static String
Shortcut forSimpleDateFormat
to output a String representation of this calendar instance.static String
Create a String representation of this date according to the given format pattern.static String
Create a String representation of this date according to the given format pattern and timezone.static int
Support the subscript operator for a Calendar.getAt
(Calendar self, Collection fields) Support the subscript operator for a Calendar with a collection of indices.static int
Support the subscript operator for a Date.getAt
(Date self, Collection fields) Support the subscript operator for a Date with a collection of indices.static String
getDateString
(Date self) Return a string representation of the 'day' portion of this date according to the locale-specificDateFormat.SHORT
default format.static String
getDateTimeString
(Date self) Return a string representation of the date and time portion of this Date instance, according to the locale-specific format used byDateFormat
.static String
getTimeString
(Date self) Return a string representation of the time portion of this date according to the locale-specificDateFormat.MEDIUM
default format.static Date
Subtract a number of days from this date and returns the new date.static Timestamp
Subtract a number of days from this Timestamp and returns the new Timestamp object.static int
Subtract another date from this one and return the number of days of the difference.static Date
Subtract a number of days from this date and returns the new date.static int
Subtract another Date from this one and return the number of days of the difference.static Date
Increment a java.sql.Date by one day.static Calendar
Increment a Calendar by one day.static Date
Increment a Date by one day.static Date
Add a number of days to this date and returns the new date.static Timestamp
Add number of days to this Timestamp and returns the new Timestamp object.static Date
Add a number of days to this date and returns the new date.static Date
Decrement a java.sql.Date by one day.static Calendar
Decrement a Calendar by one day.static Date
Decrement a Date by one day.static void
Support the subscript operator for mutating a Calendar.static void
Support the subscript operator for mutating a Date.static void
Support mutating a Calendar with a Map.static void
Support mutating a Date with a Map.static Calendar
toCalendar
(Date self) Convert a Date to a Calendar.static Calendar
Legacy alias for copyWith.static Date
Legacy alias for copyWith.static void
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
Iterates from this date up to the given date, inclusive, incrementing by one day each time.
-
Method Details
-
getAt
Support the subscript operator for a Date.- Parameters:
self
- a Datefield
- a Calendar field, e.g. MONTH- Returns:
- the value for the given field, e.g. FEBRUARY
- Since:
- 1.5.5
- See Also:
-
getAt
Support the subscript operator for a Date with a collection of indices.- Parameters:
self
- a Datefields
- a collection of Calendar fields, e.g. [YEAR, MONTH]- Returns:
- the value for the given field, e.g. [2022, FEBRUARY]
- Since:
- 4.0.5
- See Also:
-
toCalendar
Convert a Date to a Calendar.- Parameters:
self
- a Date- Returns:
- a Calendar corresponding to the given Date
- Since:
- 1.7.6
-
getAt
Support the subscript operator for a Calendar.- Parameters:
self
- a Calendarfield
- a Calendar field, e.g. MONTH- Returns:
- the value for the given field, e.g. FEBRUARY
- Since:
- 1.7.3
- See Also:
-
getAt
Support the subscript operator for a Calendar with a collection of indices.- Parameters:
self
- a Calendarfields
- a collection of Calendar fields, e.g. [YEAR, MONTH]- Returns:
- the value for the given field, e.g. [2022, FEBRUARY]
- Since:
- 4.0.5
- See Also:
-
putAt
Support the subscript operator for mutating a Calendar. Example usage:import static java.util.Calendar.* def cal = Calendar.instance cal[DAY_OF_WEEK] = MONDAY cal[MONTH] = MARCH println cal.time // A Monday in March
- Parameters:
self
- A Calendarfield
- A Calendar field, e.g. MONTHvalue
- The value for the given field, e.g. FEBRUARY- Since:
- 1.7.3
- See Also:
-
putAt
Support the subscript operator for mutating a Date.- Parameters:
self
- A Datefield
- A Calendar field, e.g. MONTHvalue
- The value for the given field, e.g. FEBRUARY- Since:
- 1.7.3
- See Also:
-
set
Support mutating a Calendar with a Map.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:Calendar index values 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
- Parameters:
self
- A Calendarupdates
- A Map of Calendar keys and values- Since:
- 1.7.3
- See Also:
-
updated
Legacy alias for copyWith. Will be deprecated and removed in future versions of Groovy.- Since:
- 1.7.3
- See Also:
-
copyWith
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.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
- Parameters:
self
- A Calendarupdates
- A Map of Calendar keys and values- Returns:
- The newly created Calendar
- Since:
- 2.2.0
- See Also:
-
set
Support mutating a Date with a Map.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:Calendar index values 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
- Parameters:
self
- A Dateupdates
- A Map of Calendar keys and values- Since:
- 1.7.3
- See Also:
-
updated
Legacy alias for copyWith. Will be deprecated and removed in future versions of Groovy.- Since:
- 1.7.3
- See Also:
-
copyWith
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.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
- Parameters:
self
- A Dateupdates
- A Map of Calendar keys and values- Returns:
- The newly created Date
- Since:
- 2.2.0
- See Also:
-
next
Increment a Date by one day.- Parameters:
self
- a Date- Returns:
- the next days date
- Since:
- 1.0
-
next
Increment a Calendar by one day.- Parameters:
self
- a Calendar- Returns:
- a new Calendar set to the next day
- Since:
- 1.8.7
-
previous
Decrement a Calendar by one day.- Parameters:
self
- a Calendar- Returns:
- a new Calendar set to the previous day
- Since:
- 1.8.7
-
next
Increment a java.sql.Date by one day.- Parameters:
self
- a java.sql.Date- Returns:
- the next days date
- Since:
- 1.0
-
previous
Decrement a Date by one day.- Parameters:
self
- a Date- Returns:
- the previous days date
- Since:
- 1.0
-
previous
Decrement a java.sql.Date by one day.- Parameters:
self
- a java.sql.Date- Returns:
- the previous days date
- Since:
- 1.0
-
plus
Add a number of days to this date and returns the new date.- Parameters:
self
- a Datedays
- the number of days to increase- Returns:
- the new date
- Since:
- 1.0
-
plus
Add a number of days to this date and returns the new date.- Parameters:
self
- a java.sql.Datedays
- the number of days to increase- Returns:
- the new date
- Since:
- 1.0
-
plus
Add number of days to this Timestamp and returns the new Timestamp object.- Parameters:
self
- a Timestampdays
- the number of days to increase- Returns:
- the new Timestamp
-
minus
Subtract a number of days from this date and returns the new date.- Parameters:
self
- a Datedays
- the number of days to subtract- Returns:
- the new date
- Since:
- 1.0
-
minus
Subtract a number of days from this date and returns the new date.- Parameters:
self
- a java.sql.Datedays
- the number of days to subtract- Returns:
- the new date
- Since:
- 1.0
-
minus
Subtract a number of days from this Timestamp and returns the new Timestamp object.- Parameters:
self
- a Timestampdays
- the number of days to subtract- Returns:
- the new Timestamp
-
minus
Subtract another date from this one and return the number of days of the difference.Date self = Date then + (Date self - Date then)
IOW, if self is before then the result is a negative value.
- Parameters:
self
- a Calendarthen
- another Calendar- Returns:
- number of days
- Since:
- 1.6.0
-
minus
Subtract another Date from this one and return the number of days of the difference.Date self = Date then + (Date self - Date then)
IOW, if self is before then the result is a negative value.
- Parameters:
self
- a Datethen
- another Date- Returns:
- number of days
- Since:
- 1.6.0
-
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 forSimpleDateFormat
for format pattern use.Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
- Parameters:
self
- a Dateformat
- the format pattern to use according toSimpleDateFormat
- Returns:
- a string representation of this date.
- Since:
- 1.5.7
- See Also:
-
format
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 forSimpleDateFormat
for format pattern use.Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
- Parameters:
self
- a Dateformat
- the format pattern to use according toSimpleDateFormat
tz
- the TimeZone to use- Returns:
- a string representation of this date.
- Since:
- 1.8.3
- See Also:
-
getDateString
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 bedd/MM/yy
.Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
- Parameters:
self
- a Date- Returns:
- a string representation of this date
- Since:
- 1.5.7
- See Also:
-
getTimeString
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 beHH:MM:ss
.Note that a new DateFormat instance is created for every invocation of this method (for thread safety).
- Parameters:
self
- a Date- Returns:
- a string representing the time portion of this date
- Since:
- 1.5.7
- See Also:
-
getDateTimeString
Return a string representation of the date and time portion of this Date instance, according to the locale-specific format used by
DateFormat
. This method uses theDateFormat.SHORT
preset for the day portion andDateFormat.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).
- Parameters:
self
- a Date- Returns:
- a string representation of this date and time
- Since:
- 1.5.7
- See Also:
-
clearTime
Clears the time portion of this Date instance; useful utility where it makes sense to compare month/day/year only portions of a Date.- Parameters:
self
- a Date- Returns:
- the Date but with the time portion cleared
- Since:
- 1.6.7
-
clearTime
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.- Parameters:
self
- a java.sql.Date- Returns:
- the java.sql.Date but with the time portion cleared
- Since:
- 1.6.7
-
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.- Parameters:
self
- a Calendar- Returns:
- the Calendar but with the time portion cleared
- Since:
- 1.6.7
-
format
Shortcut for
SimpleDateFormat
to output a String representation of this calendar instance. This method respects the Calendar's assignedTimeZone
, whereas callingcal.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 aLocale
, 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 callDateFormat.getDateInstance(int, java.util.Locale)
and pass the same Locale that was used for the Calendar.- Parameters:
self
- this calendarpattern
- format pattern- Returns:
- String representation of this calendar with the given format.
- Since:
- 1.6.0
- See Also:
-
upto
Iterates from this date up to the given date, inclusive, incrementing by one day each time.- Parameters:
self
- a Dateto
- another Date to go up toclosure
- the closure to call- Since:
- 2.2
-
upto
Iterates from the date represented by this calendar up to the date represented by the given calendar, inclusive, incrementing by one day each time.- Parameters:
self
- a Calendarto
- another Calendar to go up toclosure
- the closure to call- Since:
- 2.2
-
downto
Iterates from this date down to the given date, inclusive, decrementing by one day each time.- Parameters:
self
- a Dateto
- another Date to go down toclosure
- the closure to call- Since:
- 2.2
-
downto
Iterates from the date represented by this calendar up to the date represented by the given calendar, inclusive, incrementing by one day each time.- Parameters:
self
- a Calendarto
- another Calendar to go down toclosure
- the closure to call- Since:
- 2.2
-