public final class DateTime extends Object implements Serializable
Implementation is immutable and therefore thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
DateTime.SecondsAndNanos
A timestamp represented as the number of seconds and nanoseconds since Epoch.
|
Constructor and Description |
---|
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. |
DateTime(Date value)
|
DateTime(Date date,
TimeZone zone)
|
DateTime(long value)
Instantiates
DateTime from the number of milliseconds since the Unix epoch. |
DateTime(long value,
int tzShift)
Instantiates
DateTime from the number of milliseconds since the Unix epoch, and a shift
from UTC in minutes. |
DateTime(String value)
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o) |
int |
getTimeZoneShift()
Returns the time zone shift from UTC in minutes or
0 for date-only value. |
long |
getValue()
Returns the date/time value expressed as the number of milliseconds since the Unix epoch.
|
int |
hashCode() |
boolean |
isDateOnly()
Returns whether this is a date-only value.
|
static DateTime |
parseRfc3339(String str)
Parses an RFC3339 date/time value.
|
static DateTime.SecondsAndNanos |
parseRfc3339ToSecondsAndNanos(String str)
Parses an RFC3339 timestamp to a pair of seconds and nanoseconds since Unix Epoch.
|
String |
toString() |
String |
toStringRfc3339()
Formats the value as an RFC 3339 date/time string.
|
public DateTime(Date date, TimeZone zone)
date
- date and timezone
- time zone; if null
, it is interpreted as TimeZone.getDefault()
.public DateTime(long value)
DateTime
from the number of milliseconds since the Unix epoch.
The time zone is interpreted as TimeZone.getDefault()
, which may vary with
implementation.
value
- number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)public DateTime(Date value)
DateTime
from a Date
.
The time zone is interpreted as TimeZone.getDefault()
, which may vary with
implementation.
value
- date and timepublic DateTime(long value, int tzShift)
DateTime
from the number of milliseconds since the Unix epoch, and a shift
from UTC in minutes.value
- number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)tzShift
- time zone, represented by the number of minutes off of UTC.public DateTime(boolean dateOnly, long value, Integer tzShift)
DateTime
, which may represent a date-only value, from the number of
milliseconds since the Unix epoch, and a shift from UTC in minutes.dateOnly
- specifies if this should represent a date-only valuevalue
- number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)tzShift
- time zone, represented by the number of minutes off of UTC, or null
for
TimeZone.getDefault()
.public DateTime(String value)
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.
value
- an RFC 3339 date/time value.public long getValue()
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.
public boolean isDateOnly()
public int getTimeZoneShift()
0
for date-only value.public String toStringRfc3339()
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()
.
public static DateTime parseRfc3339(String str)
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.
str
- Date/time string in RFC3339 formatNumberFormatException
- if str
doesn't match the RFC3339 standard format; an
exception is thrown if str
doesn't match RFC3339_REGEX
or if it contains a
time zone shift but no time.public static DateTime.SecondsAndNanos parseRfc3339ToSecondsAndNanos(String str)
str
- Date/time string in RFC3339 formatIllegalArgumentException
- if str
doesn't match the RFC3339 standard format; an
exception is thrown if str
doesn't match RFC3339_REGEX
or if it contains a
time zone shift but no time.Copyright © 2011–2023 Google. All rights reserved.