Class DateTime
- All Implemented Interfaces:
Serializable
Implementation is immutable and therefore thread-safe.
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Result of parsing an RFC 3339 string.static final class
A timestamp represented as the number of seconds and nanoseconds since Epoch. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final boolean
Specifies whether this is a date-only value.private static final TimeZone
private static final Pattern
private static final String
Regular expression for parsing RFC3339 date/times.private static final long
private final int
Time zone shift from UTC in minutes or0
for date-only value.private final long
Date/time value expressed as the number of ms since the Unix epoch. -
Constructor Summary
ConstructorsConstructorDescriptionInstantiatesDateTime
, which may represent a date-only value, from the number of milliseconds since the Unix epoch, and a shift from UTC in minutes.DateTime
(long value) InstantiatesDateTime
from the number of milliseconds since the Unix epoch.DateTime
(long value, int tzShift) InstantiatesDateTime
from the number of milliseconds since the Unix epoch, and a shift from UTC in minutes. -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
appendInt
(StringBuilder sb, int num, int numDigits) Appends a zero-padded number to a string builder.boolean
int
Returns the time zone shift from UTC in minutes or0
for date-only value.long
getValue()
Returns the date/time value expressed as the number of milliseconds since the Unix epoch.int
hashCode()
boolean
Returns whether this is a date-only value.static DateTime
parseRfc3339
(String str) Parses an RFC3339 date/time value.static DateTime.SecondsAndNanos
Parses an RFC3339 timestamp to a pair of seconds and nanoseconds since Unix Epoch.private static DateTime.Rfc3339ParseResult
toString()
Formats the value as an RFC 3339 date/time string.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
GMT
-
RFC3339_REGEX
Regular expression for parsing RFC3339 date/times.- See Also:
-
RFC3339_PATTERN
-
value
private final long valueDate/time value expressed as the number of ms 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.
-
dateOnly
private final boolean dateOnlySpecifies whether this is a date-only value. -
tzShift
private final int tzShiftTime zone shift from UTC in minutes or0
for date-only value.
-
-
Constructor Details
-
DateTime
- Parameters:
date
- date and timezone
- time zone; ifnull
, it is interpreted asTimeZone.getDefault()
.
-
DateTime
public DateTime(long value) InstantiatesDateTime
from the number of milliseconds since the Unix epoch.The time zone is interpreted as
TimeZone.getDefault()
, which may vary with implementation.- Parameters:
value
- number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT)
-
DateTime
InstantiatesDateTime
from aDate
.The time zone is interpreted as
TimeZone.getDefault()
, which may vary with implementation.- Parameters:
value
- date and time
-
DateTime
public DateTime(long value, int tzShift) InstantiatesDateTime
from the number of milliseconds since the Unix epoch, and a shift from UTC in minutes.- Parameters:
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.
-
DateTime
InstantiatesDateTime
, which may represent a date-only value, from the number of milliseconds since the Unix epoch, and a shift from UTC in minutes.- Parameters:
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, ornull
forTimeZone.getDefault()
.
-
DateTime
InstantiatesDateTime
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.- Parameters:
value
- an RFC 3339 date/time value.- Since:
- 1.11
-
-
Method Details
-
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.
- Since:
- 1.5
-
isDateOnly
public boolean isDateOnly()Returns whether this is a date-only value.- Since:
- 1.5
-
getTimeZoneShift
public int getTimeZoneShift()Returns the time zone shift from UTC in minutes or0
for date-only value.- Since:
- 1.5
-
toStringRfc3339
Formats the value as an RFC 3339 date/time string. -
toString
-
equals
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()
. -
hashCode
public int hashCode() -
parseRfc3339
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.
- Parameters:
str
- Date/time string in RFC3339 format- Throws:
NumberFormatException
- ifstr
doesn't match the RFC3339 standard format; an exception is thrown ifstr
doesn't matchRFC3339_REGEX
or if it contains a time zone shift but no time.
-
parseRfc3339ToSecondsAndNanos
Parses an RFC3339 timestamp to a pair of seconds and nanoseconds since Unix Epoch.- Parameters:
str
- Date/time string in RFC3339 format- Throws:
IllegalArgumentException
- ifstr
doesn't match the RFC3339 standard format; an exception is thrown ifstr
doesn't matchRFC3339_REGEX
or if it contains a time zone shift but no time.
-
parseRfc3339WithNanoSeconds
private static DateTime.Rfc3339ParseResult parseRfc3339WithNanoSeconds(String str) throws NumberFormatException - Throws:
NumberFormatException
-
appendInt
Appends a zero-padded number to a string builder.
-