Date and Time Operators

You can use the following operators for date, time, and dateTime data types.

Operator Description

+

Addition (use only when the second argument is a duration)

-

Subtraction (use only when the second argument is a duration)

==

Equals

!=

Not Equals

>

Greater than

>=

Greater than or Equals

<

Less than

<=

Less than or Equals

format

Returns the formatted string of date-time using the provided XSLT 2.0 format picture.

Examples:

  • travelDate.format("[M01]/[D01]/[Y0001] [H01]:[m01] [z]") returns "02/24/2020 18:37 GMT+00:00"
  • travelDate.format("[h1]:[m01] [P] on [MNn] [D]") returns "6:37 pm on February 24"

+

Addition

-

Subtraction

==

Equals

Date Operators

In addition, there are some specific operators that you can use on each of date, time, and dateTime data types.

Date Operator Description

year

Returns a number representing the year component of the date-time argument.

month

Returns a number representing the month component of the date-time argument.

day

Returns a number representing the day component of the date-time argument.

Time Operators

Time Operator Description

hours

Returns a number between 0 and 23, both inclusive, representing the hours component of the date-time argument.

minutes

Returns a number between 0 and 59, both inclusive, representing the minutes component of the date-time argument.

seconds

Returns a number between 0 and 59, both inclusive, representing the seconds component of the date-time argument.

timezone

Returns an interval value, representing the time offset from UTC.

Date-Time Operators

Date-Time Operator Description

year

Returns a number representing the year component of the date-time argument.

month

Returns a number representing the month component of the date-time argument.

day

Returns a number representing the day component of the date-time argument.

hours

Returns a number between 0 and 23, both inclusive, representing the hours component of the date-time argument.

minutes

Returns a number between 0 and 59, both inclusive, representing the minutes component of the date-time argument.

seconds

Returns a number between 0 and 59, both inclusive, representing the seconds component of the date-time argument.

timezone

Returns an interval value, representing the time offset from UTC.

toTimezone

Returns the date-time expressed in the time offset corresponding to the timezone ID provided.

You have the following options for specifying a timezone ID:
  • Provide a fixed offset from UTC/Greenwich, such as "-07:00".

    For example:

    • dateTimeDO.toTimezone("-07:00") returns 2002-11-30T17:20:00-07:00 when dateTimeDO is 2002-12-01T01:20:00+01:00
  • Specify a geographical region, which is an area where a specific set of rules for finding the offset from UTC/Greenwich apply. A geographical region is usually represented in the format "{area}/{city}", such as "Europe/Amsterdam" or "America/New_York".

    For example:

    • dateTimeDO.toTimezone("America/Los_Angeles") returns 2002-11-30T16:20:00-08:00 when dateTimeDO is 2002-12-01T01:20:00+01:00
    • dateTimeDO.toTimezone("America/Los_Angeles") returns 2002-06-30T17:20:00-07:00 when dateTimeDO is 2002-07-01T01:20:00+01:00

Note: You must include double quotation marks around the timezone ID value.

For more information about timezone IDs, see Class ZoneId in the Java Platform documentation.