Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Current »

We use class Date for simple date processing:

Other date and time processing can be done using the GregorianCalendar class:

GregorianCalendar supports both the Julian and Gregorian calendar systems, supporting one discontinuity, which by default is when the Gregorian calendar was first instituted in some countries, ie, 4 October 1582 (Julian) followed by 15 October, 1582 (Gregorian). The only difference between the calendars is the leap year rule: the Julian specifies leap years every four years, whereas the Gregorian omits century years which are not divisible by 400. Because dates are computed by extrapolating the current rules indefinitely far backward and forward in time, this calendar generates consistent results for all years, although dates obtained are historically accurate only from March 1, 4 AD onward, when modern Julian calendar rules were adopted. Although New Year's Day was March 25 prior to the institution of the Gregorian calendar, to avoid confusion, this calendar always uses January 1.

From Groovy 1.5.7 / 1.6.x,  you may use Date.format() directly. Refer to GROOVY-3066 for details.

Alternatively, Dates and times can be formatted easily with String.format(). The first character is 't' or 'T' for each item:


After setting fields, we must call any get(), add(), or roll() method, or access the 'timeInMillis' or 'time' properties, to cause other relevant fields to update themselves:

We can also set the time in this way:

More field manipulations:

We can compare dates:

As well as using set(), calendar fields can be changed using add() and roll(), both of which force all fields to update themselves:

We can turn off the lenient mode for field updates to force us to give calendars precisely correct values:

Durations

We can use durations:

These durations can be created more easily within the TimeCategory:

We can add and subtract durations of different types together:

We can add a Date to a duration to give another Date. A TimeDuration takes leap minutes into account, a Duration also takes daylight saving into account, and a DatumDependentDuration considers 31-day months and leap-years:

Time Zones

We can retrieve lists of all time zones on a system:

We can access various time zones individually:

We can create a time zone with custom daylight-saving time rules:

(Coordinated universal time, UTC, being based on an atomic clock, enables an extra second, a "leap second", to be added as the last second of the day on December 31 or June 30.)

We can use time zones in many various ways:

  • No labels