Class DateTime
- All Implemented Interfaces:
Externalizable
,Serializable
,Comparable<DateTime>
public final class DateTime extends Object implements Comparable<DateTime>, Externalizable
The DateTime represents a zone-less, precise timepoint without respect to timezones. The instant is stored as a
signed 64-bit long, representing nanoseconds since the epoch (January 1, 1970, 00:00:00 GMT). This provides a range
from 1677-09-21T00:12:43.146-775807 UTC to 2262-04-11T23:47:16.854775807 UTC. The minimum long value is reserved for
QueryConstants.NULL_LONG
and therefore is not permitted as a valid DateTime.
- See Also:
- Serialized Form
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description int
compareTo(DateTime dateTime)
boolean
equals(Object that)
Date
getDate()
Deprecated.Instant
getInstant()
Convert this DateTime to a Java Instant.org.joda.time.DateTime
getJodaDateTime()
Deprecated.usetoZonedDateTime(ZoneId)
insteadorg.joda.time.DateTime
getJodaDateTime(TimeZone timeZone)
Deprecated.usetoZonedDateTime(ZoneId)
insteadlong
getMicros()
Get this time represented as microseconds since the epochlong
getMillis()
Get this time represented as milliseconds since the epochlong
getNanos()
Get this time represented as nanoseconds since the epochlong
getNanosPartial()
Get nanoseconds-of-milliseconds; this number will always be between 0 and 999,999int
hashCode()
static DateTime
now()
Create a new DateTime initialized to the current system time.static DateTime
nowMillis()
Create a new DateTime initialized to the current system time.static DateTime
of(Clock clock)
Create a new date time viaClock.currentTimeNanos()
.static DateTime
of(Instant instant)
static DateTime
ofMillis(Clock clock)
Create a new date time viaClock.currentTimeMillis()
.void
readExternal(ObjectInput in)
String
toDateString()
Get the date represented by this DateTime in the defaultTimeZone
.String
toDateString(TimeZone timeZone)
Get the date represented by this DateTime in the givenTimeZone
.String
toDateString(String zoneId)
Get the date represented by this DateTime in the time zone specified byzoneId
inISO
date format.String
toDateString(ZoneId timeZone)
Get the date represented by this DateTime in the given javaZoneId
inISO
date format.String
toDateString(org.joda.time.DateTimeZone timeZone)
Get the date represented by this DateTime in the given JodaDateTimeZone
in ISO date format yyyy-mm.LocalDate
toLocalDate()
LocalDate
toLocalDate(TimeZone zone)
LocalDate
toLocalDate(String zone)
LocalDate
toLocalDate(ZoneId zone)
LocalTime
toLocalTime()
LocalTime
toLocalTime(TimeZone zone)
LocalTime
toLocalTime(String zone)
LocalTime
toLocalTime(ZoneId zone)
String
toString()
String
toString(TimeZone timeZone)
Convert this DateTime into a String using the providedTimeZone
.ZonedDateTime
toZonedDateTime()
ZonedDateTime
toZonedDateTime(TimeZone zone)
Get aZonedDateTime
version of thisDateTime
at the specified time zone.ZonedDateTime
toZonedDateTime(String zone)
Get aZonedDateTime
version of thisDateTime
at the specified time zone.ZonedDateTime
toZonedDateTime(ZoneId zone)
Get aZonedDateTime
version of thisDateTime
at the specified time zone.void
writeExternal(ObjectOutput out)
-
Constructor Details
-
DateTime
public DateTime()Create a new DateTime initialized to the epoch. -
DateTime
public DateTime(long nanos)Create a new DateTime initialized to the provided nanoseconds since the epoch.- Parameters:
nanos
- the number of nanoseconds since the epoch
-
-
Method Details
-
of
-
of
Create a new date time viaClock.currentTimeNanos()
.Equivalent to
new DateTime(clock.currentTimeNanos())
.If nanosecond resolution is not necessary, consider using
ofMillis(Clock)
.- Parameters:
clock
- the clock- Returns:
- the date time
-
ofMillis
Create a new date time viaClock.currentTimeMillis()
.Equivalent to
new DateTime(Math.multiplyExact(clock.currentTimeMillis(), 1_000_000))
.- Parameters:
clock
- the clock- Returns:
- the date time
-
now
Create a new DateTime initialized to the current system time. Based onClock.system()
. Equivalent toof(Clock.system())
.The precision of DateTime is nanoseconds, but the resolution of the this method depends on the JVM.
If you don't need nanosecond resolution, it may be preferable to use
nowMillis()
.Note: overflow checking is not performed - this method will overflow in the year 2262.
- Returns:
- a new DateTime initialized to the current time.
-
nowMillis
Create a new DateTime initialized to the current system time. Based onClock.system()
. Equivalent toofMillis(Clock.system())
.The resolution will be in milliseconds.
- Returns:
- a new DateTime initialized to the current time.
-
getNanos
public long getNanos()Get this time represented as nanoseconds since the epoch- Returns:
- the number of nanoseconds since the epoch
-
getMicros
public long getMicros()Get this time represented as microseconds since the epoch- Returns:
- the number of microseconds since the epoch
-
getMillis
public long getMillis()Get this time represented as milliseconds since the epoch- Returns:
- the number of milliseconds since the epoch
-
getNanosPartial
public long getNanosPartial()Get nanoseconds-of-milliseconds; this number will always be between 0 and 999,999- Returns:
- the number of nanoseconds after the nearest millisecond.
-
getJodaDateTime
Deprecated.usetoZonedDateTime(ZoneId)
insteadConvert this DateTime to a Joda DateTime. This DateTime will be truncated to milliseconds.- Returns:
- a Joda DateTime representing this DateTime
-
getJodaDateTime
Deprecated.usetoZonedDateTime(ZoneId)
insteadConvert this DateTime to a Joda DateTime. This DateTime will be truncated to milliseconds.- Parameters:
timeZone
- the timezone for the created Joda DateTime- Returns:
- a Joda DateTime representing this DateTime
-
toZonedDateTime
- Returns:
- a
ZonedDateTime
-
toZonedDateTime
Get aZonedDateTime
version of thisDateTime
at the specified time zone.- Returns:
- a
ZonedDateTime
-
toZonedDateTime
Get aZonedDateTime
version of thisDateTime
at the specified time zone.- Returns:
- a
ZonedDateTime
-
toZonedDateTime
Get aZonedDateTime
version of thisDateTime
at the specified time zone.- Returns:
- a
ZonedDateTime
-
toLocalDate
- Returns:
- the
LocalDate
-
toLocalDate
- Returns:
- the
LocalDate
-
toLocalDate
- Returns:
- the
LocalDate
-
toLocalDate
- Returns:
- the
LocalDate
-
toLocalTime
- Returns:
- the
LocalTime
-
toLocalTime
- Returns:
- the
LocalTime
-
toLocalTime
- Returns:
- the
LocalTime
-
toLocalTime
- Returns:
- the
LocalTime
-
getDate
Deprecated.usetoZonedDateTime()
instead.Convert this DateTime to a Java Date. This DateTime will be truncated to milliseconds.- Returns:
- a Java Date representing this DateTime
-
getInstant
Convert this DateTime to a Java Instant.- Returns:
- a Java Instant representing this DateTime
-
equals
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareTo
in interfaceComparable<DateTime>
-
toString
-
toString
Convert this DateTime into a String using the providedTimeZone
.The date will be formatted as
yyyy-MM-DDThh:mm:ss.SSSSSSSSS TZ
, for example2020-05-27T13:37:57.780853000 NY
or2020-05-27T17:37:42.763641000 UTC
.- Parameters:
timeZone
- the timezone for formatting the string- Returns:
- a String representation of this DateTime
-
toDateString
Get the date represented by this DateTime in the defaultTimeZone
.- Returns:
- The date (yyyy-MM-dd) represented by this
DateTime
in the defaultTimeZone
.
-
toDateString
Get the date represented by this DateTime in the givenTimeZone
.- Parameters:
timeZone
- a TimeZone- Returns:
- The date (yyyy-MM-dd) represented by this
DateTime
in the given timeZone.
-
toDateString
Get the date represented by this DateTime in the given JodaDateTimeZone
in ISO date format yyyy-mm.- Parameters:
timeZone
- A joda DateTimeZone- Returns:
- The date (yyyy-MM-dd) represented by this
DateTime
in the giventimeZone
-
toDateString
Get the date represented by this DateTime in the time zone specified byzoneId
inISO
date format.- Parameters:
zoneId
- A java time zone ID string- Returns:
- The date (yyyy-MM-dd) represented by this
DateTime
in time zone represented by the givenzoneId
-
toDateString
Get the date represented by this DateTime in the given javaZoneId
inISO
date format.- Parameters:
timeZone
- A javatime zone ID
.- Returns:
- The date (yyyy-MM-dd) represented by this
DateTime
in the giventimeZone
-
writeExternal
- Specified by:
writeExternal
in interfaceExternalizable
- Throws:
IOException
-
readExternal
- Specified by:
readExternal
in interfaceExternalizable
- Throws:
IOException
-
toZonedDateTime()
instead.