Groovy JDK

java.util
Class Date

Method Summary
Date clearTime()
Clears the time portion of this Date instance; Util where it makes sense to compare month/day/year only portions of a Date
String format(String format)

Create a String representation of this date according to the given format pattern.

String format(String format, TimeZone tz)

Create a String representation of this date according to the given format pattern and timezone.

int getAt(int field)
Support the subscript operator for a Date.
String getDateString()

Return a string representation of the 'day' portion of this date according to the locale-specific DateFormat#SHORT default format.

String getDateTimeString()

Return a string representation of the date and time time portion of this Date instance, according to the locale-specific format used by DateFormat.

String getTimeString()

Return a string representation of the time portion of this date according to the locale-specific DateFormat#MEDIUM default format.

Date minus(int days)
Subtract a number of days from this date and returns the new date.
int minus(Date then)
Subtract another Date from this one and return the number of days of the difference.
Date next()
Increment a Date by one day.
static Date parse(String format, String input)
Parse a String into a Date instance using the given pattern.
static Date parseToStringDate(String dateToString)
Parse a String matching the pattern EEE MMM dd HH:mm:ss zzz yyyy containing US-locale-constants only (e.g.
Date plus(int days)
Add a number of days to this date and returns the new date.
Date previous()
Decrement a Date by one day.
void putAt(int field, int value)
Support the subscript operator for mutating a Date.
void set(Map updates)
Support mutating a Date with a Map.
Calendar toCalendar()
Convert a Date to a Calendar.
Timestamp toTimestamp()
Return a java.sql.Timestamp given a java.util.Date.
Date 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

clearTime

public Date clearTime()
 
Clears the time portion of this Date instance; Util where it makes sense to compare month/day/year only portions of a Date
Returns:
the Date but with the time portion cleared
Since:
1.6.7

format

public String format(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).

Parameters:
format - the format pattern to use according to SimpleDateFormat.
Returns:
a string representation of this date.
Since:
1.5.7
See:
SimpleDateFormat.

format

public String format(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).

Parameters:
format - the format pattern to use according to SimpleDateFormat.
tz - the TimeZone to use.
Returns:
a string representation of this date.
Since:
1.8.3
See:
SimpleDateFormat.

getAt

public int getAt(int field)
 
Support the subscript operator for a Date.
Parameters:
field - a Calendar field, e.g. MONTH.
Returns:
the value for the given field, e.g. FEBRUARY
Since:
1.5.5
See:
Calendar.

getDateString

public String 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 be dd/MM/yy.

Note that a new DateFormat instance is created for every invocation of this method (for thread safety).

Returns:
a string representation of this date
Since:
1.5.7
See:
DateFormat#getDateInstance.
DateFormat#SHORT.

getDateTimeString

public String getDateTimeString()
 

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).

Returns:
a string representation of this date and time
Since:
1.5.7
See:
DateFormat#getDateTimeInstance.

getTimeString

public String 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 be HH:MM:ss.

Note that a new DateFormat instance is created for every invocation of this method (for thread safety).

Returns:
a string representing the time portion of this date
Since:
1.5.7
See:
DateFormat#getTimeInstance.
DateFormat#MEDIUM.

minus

public Date minus(int days)
 
Subtract a number of days from this date and returns the new date.
Parameters:
days - the number of days to subtract.
Returns:
the new date
Since:
1.0

minus

public int minus(Date then)
 
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:
then - another Date.
Returns:
number of days
Since:
1.6.0

next

public Date next()
 
Increment a Date by one day.
Returns:
the next days date
Since:
1.0

parse

public static Date parse(String format, String input)
 
Parse a String into a Date instance using the given pattern. This convenience method acts as a wrapper for SimpleDateFormat.

Note that a new SimpleDateFormat instance is created for every invocation of this method (for thread safety).

Parameters:
format - pattern used to parse the input string..
input - String to be parsed to create the date instance.
Returns:
a new Date instance representing the parsed input string
Since:
1.5.7
See:
SimpleDateFormat#parse.

parseToStringDate

public static Date parseToStringDate(String dateToString)
 
Parse a String matching the pattern EEE MMM dd HH:mm:ss zzz yyyy containing US-locale-constants only (e.g. Sat for Saturdays). Such a string is generated by the toString method of Date

Note that a new SimpleDateFormat instance is created for every invocation of this method (for thread safety).

Parameters:
dateToString - String to be parsed to create the date instance. Must match the pattern EEE MMM dd HH:mm:ss zzz yyyy with US-locale symbols.
Returns:
a new Date instance representing the parsed input string

plus

public Date plus(int days)
 
Add a number of days to this date and returns the new date.
Parameters:
days - the number of days to increase.
Returns:
the new date
Since:
1.0

previous

public Date previous()
 
Decrement a Date by one day.
Returns:
the previous days date
Since:
1.0

putAt

public void putAt(int field, int value)
 
Support the subscript operator for mutating a Date.
Parameters:
field - A Calendar field, e.g. MONTH.
value - The value for the given field, e.g. FEBRUARY.
Since:
1.7.3
See:
Calendar#putAt.
Calendar#set.

set

public void set(Map updates)
 
Support mutating a Date with a Map.

Example usage:

import static java.util.Calendar.YEAR
def date = new Date()
def nextYear = date[YEAR] + 1
date.set(year: nextYear)
println date
Parameters:
updates - A Map of Calendar keys and values.
Since:
1.7.3
See:
Calendar#set.
Calendar#set.

toCalendar

public Calendar toCalendar()
 
Convert a Date to a Calendar.
Returns:
a Calendar corresponding to the given Date
Since:
1.7.6

toTimestamp

public Timestamp toTimestamp()
 
Return a java.sql.Timestamp given a java.util.Date.
Returns:
the date wrapped as a Timestamp
Since:
1.6.6

updated

public Date 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.

Example usage:

import static java.util.Calendar.YEAR
def today = new Date()
def nextYear = today[YEAR] + 1
def oneYearFromNow = today.updated(year: nextYear)
println today
println oneYearFromNow
Parameters:
updates - A Map of Calendar keys and values.
Returns:
The newly created Date
Since:
1.7.3
See:
Calendar#set.
Date#set.
Calendar#set.
Calendar#updated.

Groovy JDK