Class DateTime (1.45.0)

public final class DateTime implements Serializable

Immutable representation of a date with an optional time and an optional time zone based on RFC 3339.

Implementation is immutable and therefore thread-safe.

Inheritance

Object > DateTime

Implements

Serializable

Static Methods

parseRfc3339(String str)

public static DateTime parseRfc3339(String str)

Parses an RFC3339 date/time value.

Upgrade warning: in prior version 1.17, this method required milliseconds to be exactly 3 digits (if included), and did not throw an exception for all types of invalid input values, but starting in version 1.18, the parsing done by this method has become more strict to enforce that only valid RFC3339 strings are entered, and if not, it throws a NumberFormatException. Also, in accordance with the RFC3339 standard, any number of milliseconds digits is now allowed.

Any time information beyond millisecond precision is truncated.

For the date-only case, the time zone is ignored and the hourOfDay, minute, second, and millisecond parameters are set to zero.

Parameter
Name Description
str String

Date/time string in RFC3339 format

Returns
Type Description
DateTime

parseRfc3339ToSecondsAndNanos(String str)

public static DateTime.SecondsAndNanos parseRfc3339ToSecondsAndNanos(String str)

Parses an RFC3339 timestamp to a pair of seconds and nanoseconds since Unix Epoch.

Parameter
Name Description
str String

Date/time string in RFC3339 format

Returns
Type Description
DateTime.SecondsAndNanos

Constructors

DateTime(boolean dateOnly, long value, Integer tzShift)

public DateTime(boolean dateOnly, long value, Integer tzShift)

Instantiates DateTime, which may represent a date-only value, from the number of milliseconds since the Unix epoch, and a shift from UTC in minutes.

Parameters
Name Description
dateOnly boolean

specifies if this should represent a date-only value

value long

number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)

tzShift Integer

time zone, represented by the number of minutes off of UTC, or null for TimeZone.getDefault().

DateTime(String value)

public DateTime(String value)

Instantiates DateTime from an RFC 3339 date/time value.

Upgrade warning: in prior version 1.17, this method required milliseconds to be exactly 3 digits (if included), and did not throw an exception for all types of invalid input values, but starting in version 1.18, the parsing done by this method has become more strict to enforce that only valid RFC3339 strings are entered, and if not, it throws a NumberFormatException. Also, in accordance with the RFC3339 standard, any number of milliseconds digits is now allowed.

Parameter
Name Description
value String

an RFC 3339 date/time value.

DateTime(Date value)

public DateTime(Date value)

Instantiates DateTime from a Date.

The time zone is interpreted as TimeZone.getDefault(), which may vary with implementation.

Parameter
Name Description
value Date

date and time

DateTime(Date date, TimeZone zone)

public DateTime(Date date, TimeZone zone)

Instantiates DateTime from a Date and TimeZone.

Parameters
Name Description
date Date

date and time

zone TimeZone

time zone; if null, it is interpreted as TimeZone.getDefault().

DateTime(long value)

public DateTime(long value)

Instantiates DateTime from the number of milliseconds since the Unix epoch.

The time zone is interpreted as TimeZone.getDefault(), which may vary with implementation.

Parameter
Name Description
value long

number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)

DateTime(long value, int tzShift)

public DateTime(long value, int tzShift)

Instantiates DateTime from the number of milliseconds since the Unix epoch, and a shift from UTC in minutes.

Parameters
Name Description
value long

number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)

tzShift int

time zone, represented by the number of minutes off of UTC.

Methods

equals(Object o)

public boolean equals(Object o)

A check is added that the time zone is the same. If you ONLY want to check equality of time value, check equality on the #getValue().

Parameter
Name Description
o Object
Returns
Type Description
boolean
Overrides

getTimeZoneShift()

public int getTimeZoneShift()

Returns the time zone shift from UTC in minutes or 0 for date-only value.

Returns
Type Description
int

getValue()

public long getValue()

Returns the date/time value expressed as the number of milliseconds since the Unix epoch.

If the time zone is specified, this value is normalized to UTC, so to format this date/time value, the time zone shift has to be applied.

Returns
Type Description
long

hashCode()

public int hashCode()
Returns
Type Description
int
Overrides

isDateOnly()

public boolean isDateOnly()

Returns whether this is a date-only value.

Returns
Type Description
boolean

toString()

public String toString()
Returns
Type Description
String
Overrides

toStringRfc3339()

public String toStringRfc3339()

Formats the value as an RFC 3339 date/time string.

Returns
Type Description
String