|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport org.codehaus.groovy.runtime.DateGroovyMethods
public class DateGroovyMethods extends DefaultGroovyMethodsSupport
This class defines new groovy methods which appear on normal JDK Date and Calendar classes inside the Groovy environment.
Method Summary | |
---|---|
static java.util.Date
|
clearTime(java.util.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 java.sql.Date
|
clearTime(java.sql.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 java.util.Calendar
|
clearTime(java.util.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 java.lang.String
|
format(java.util.Date self, java.lang.String format)
|
static java.lang.String
|
format(java.util.Date self, java.lang.String format, java.util.TimeZone tz)
|
static java.lang.String
|
format(java.util.Calendar self, java.lang.String pattern)
|
static int
|
getAt(java.util.Date self, int field)
Support the subscript operator for a Date. |
static int
|
getAt(java.util.Calendar self, int field)
Support the subscript operator for a Calendar. |
static java.lang.String
|
getDateString(java.util.Date self)
|
static java.lang.String
|
getDateTimeString(java.util.Date self)
|
static java.lang.String
|
getTimeString(java.util.Date self)
|
static java.util.Date
|
minus(java.util.Date self, int days)
Subtract a number of days from this date and returns the new date. |
static java.sql.Date
|
minus(java.sql.Date self, int days)
Subtract a number of days from this date and returns the new date. |
static java.sql.Timestamp
|
minus(java.sql.Timestamp self, int days)
Subtract a number of days from this Timestamp and returns the new Timestamp object. |
static int
|
minus(java.util.Calendar self, java.util.Calendar then)
Subtract another date from this one and return the number of days of the difference. |
static int
|
minus(java.util.Date self, java.util.Date then)
Subtract another Date from this one and return the number of days of the difference. |
static java.util.Date
|
next(java.util.Date self)
Increment a Date by one day. |
static java.util.Calendar
|
next(java.util.Calendar self)
Increment a Calendar by one day. |
static java.sql.Date
|
next(java.sql.Date self)
Increment a java.sql.Date by one day. |
static java.util.Date
|
plus(java.util.Date self, int days)
Add a number of days to this date and returns the new date. |
static java.sql.Date
|
plus(java.sql.Date self, int days)
Add a number of days to this date and returns the new date. |
static java.sql.Timestamp
|
plus(java.sql.Timestamp self, int days)
Add number of days to this Timestamp and returns the new Timestamp object. |
static java.util.Calendar
|
previous(java.util.Calendar self)
Decrement a Calendar by one day. |
static java.util.Date
|
previous(java.util.Date self)
Decrement a Date by one day. |
static java.sql.Date
|
previous(java.sql.Date self)
Decrement a java.sql.Date by one day. |
static void
|
putAt(java.util.Calendar self, int field, int value)
Support the subscript operator for mutating a Calendar. |
static void
|
putAt(java.util.Date self, int field, int value)
Support the subscript operator for mutating a Date. |
static void
|
set(java.util.Calendar self, java.util.Map updates)
Support mutating a Calendar with a Map. |
static void
|
set(java.util.Date self, java.util.Map updates)
Support mutating a Date with a Map. |
static java.util.Calendar
|
toCalendar(java.util.Date self)
Convert a Date to a Calendar. |
static java.util.Calendar
|
updated(java.util.Calendar self, java.util.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. |
static java.util.Date
|
updated(java.util.Date self, java.util.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. |
Methods inherited from class java.lang.Object | |
---|---|
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Method Detail |
---|
public static java.util.Date clearTime(java.util.Date self)
self
- a Date
public static java.sql.Date clearTime(java.sql.Date self)
self
- a java.sql.Date
public static java.util.Calendar clearTime(java.util.Calendar self)
self
- a Calendar
public static java.lang.String format(java.util.Date self, java.lang.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 java.text.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 java.text.SimpleDateFormat
public static java.lang.String format(java.util.Date self, java.lang.String format, java.util.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 java.text.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 java.text.SimpleDateFormattz
- the TimeZone to use
public static java.lang.String format(java.util.Calendar self, java.lang.String pattern)
Shortcut for java.text.SimpleDateFormat to output a String representation
of this calendar instance. This method respects the Calendar's assigned
java.util.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 java.util.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
java.text.DateFormat#getDateInstance(int, java.util.Locale) and pass the same Locale
that was used for the Calendar.
self
- this calendarpattern
- format pattern
public static int getAt(java.util.Date self, int field)
self
- a Datefield
- a Calendar field, e.g. MONTH
public static int getAt(java.util.Calendar self, int field)
self
- a Calendarfield
- a Calendar field, e.g. MONTH
public static java.lang.String getDateString(java.util.Date self)
Return a string representation of the 'day' portion of this date
according to the locale-specific java.text.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 Date
public static java.lang.String getDateTimeString(java.util.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 java.text.DateFormat. This method uses the java.text.DateFormat#SHORT preset for the day portion and java.text.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 Date
public static java.lang.String getTimeString(java.util.Date self)
Return a string representation of the time portion of this date
according to the locale-specific java.text.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 Date
public static java.util.Date minus(java.util.Date self, int days)
self
- a Datedays
- the number of days to subtract
public static java.sql.Date minus(java.sql.Date self, int days)
self
- a java.sql.Datedays
- the number of days to subtract
public static java.sql.Timestamp minus(java.sql.Timestamp self, int days)
self
- a Timestampdays
- the number of days to subtract
public static int minus(java.util.Calendar self, java.util.Calendar then)
self
- a Calendarthen
- another Calendar
public static int minus(java.util.Date self, java.util.Date then)
self
- a Datethen
- another Date
public static java.util.Date next(java.util.Date self)
self
- a Date
public static java.util.Calendar next(java.util.Calendar self)
self
- a Calendar
public static java.sql.Date next(java.sql.Date self)
self
- a java.sql.Date
public static java.util.Date plus(java.util.Date self, int days)
self
- a Datedays
- the number of days to increase
public static java.sql.Date plus(java.sql.Date self, int days)
self
- a java.sql.Datedays
- the number of days to increase
public static java.sql.Timestamp plus(java.sql.Timestamp self, int days)
self
- a Timestampdays
- the number of days to increase
public static java.util.Calendar previous(java.util.Calendar self)
self
- a Calendar
public static java.util.Date previous(java.util.Date self)
self
- a Date
public static java.sql.Date previous(java.sql.Date self)
self
- a java.sql.Date
public static void putAt(java.util.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. FEBRUARY
public static void putAt(java.util.Date self, int field, int value)
self
- A Datefield
- A Calendar field, e.g. MONTHvalue
- The value for the given field, e.g. FEBRUARY
public static void set(java.util.Calendar self, java.util.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
self
- A Calendarupdates
- A Map of Calendar keys and values
public static void set(java.util.Date self, java.util.Map updates)
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 values
public static java.util.Calendar toCalendar(java.util.Date self)
self
- a Date
public static java.util.Calendar updated(java.util.Calendar self, java.util.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
self
- A Calendarupdates
- A Map of Calendar keys and values
public static java.util.Date updated(java.util.Date self, java.util.Map updates)
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
self
- A Dateupdates
- A Map of Calendar keys and values
Groovy Documentation