Class DateTime

java.lang.Object
io.deephaven.time.DateTime
All Implemented Interfaces:
Externalizable, Serializable, Comparable<DateTime>

public final class DateTime
extends Object
implements Comparable<DateTime>, Externalizable
An object representing a timepoint in Deephaven.

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 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

      public static DateTime of​(Instant instant)
    • of

      public static DateTime of​(Clock clock)
      Create a new date time via Clock.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

      public static DateTime ofMillis​(Clock clock)
      Create a new date time via Clock.currentTimeMillis().

      Equivalent to new DateTime(Math.multiplyExact(clock.currentTimeMillis(), 1_000_000)).

      Parameters:
      clock - the clock
      Returns:
      the date time
    • now

      public static DateTime now()
      Create a new DateTime initialized to the current system time. Based on Clock.system(). Equivalent to of(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

      public static DateTime nowMillis()
      Create a new DateTime initialized to the current system time. Based on Clock.system(). Equivalent to ofMillis(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 public org.joda.time.DateTime getJodaDateTime()
      Deprecated.
      Convert this DateTime to a Joda DateTime. This DateTime will be truncated to milliseconds.
      Returns:
      a Joda DateTime representing this DateTime
    • getJodaDateTime

      @Deprecated public org.joda.time.DateTime getJodaDateTime​(TimeZone timeZone)
      Deprecated.
      Convert 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

      @NotNull public ZonedDateTime toZonedDateTime()
      Get a ZonedDateTime version of this DateTime at the system default time zone.
      Returns:
      a ZonedDateTime
    • toZonedDateTime

      @NotNull public ZonedDateTime toZonedDateTime​(@NotNull String zone)
      Get a ZonedDateTime version of this DateTime at the specified time zone.
      Returns:
      a ZonedDateTime
    • toZonedDateTime

      @NotNull public ZonedDateTime toZonedDateTime​(@NotNull TimeZone zone)
      Get a ZonedDateTime version of this DateTime at the specified time zone.
      Returns:
      a ZonedDateTime
    • toZonedDateTime

      @NotNull public ZonedDateTime toZonedDateTime​(@NotNull ZoneId zone)
      Get a ZonedDateTime version of this DateTime at the specified time zone.
      Returns:
      a ZonedDateTime
    • toLocalDate

      @NotNull public LocalDate toLocalDate()
      Get a LocalDate representing the date of this DateTime at the system default time zone.
      Returns:
      the LocalDate
    • toLocalDate

      @NotNull public LocalDate toLocalDate​(@NotNull String zone)
      Get a LocalDate representing the date of this DateTime at the specified time zone.
      Returns:
      the LocalDate
    • toLocalDate

      @NotNull public LocalDate toLocalDate​(@NotNull TimeZone zone)
      Get a LocalDate representing the date of this DateTime at the specified time zone.
      Returns:
      the LocalDate
    • toLocalDate

      @NotNull public LocalDate toLocalDate​(@NotNull ZoneId zone)
      Get a LocalDate representing the date of this DateTime at the specified time zone.
      Returns:
      the LocalDate
    • toLocalTime

      @NotNull public LocalTime toLocalTime()
      Get a LocalTime representing the time of day of this DateTime at the system default time zone.
      Returns:
      the LocalTime
    • toLocalTime

      @NotNull public LocalTime toLocalTime​(@NotNull String zone)
      Get a LocalTime representing the time of day of this DateTime at the specified time zone.
      Returns:
      the LocalTime
    • toLocalTime

      @NotNull public LocalTime toLocalTime​(@NotNull TimeZone zone)
      Get a LocalTime representing the time of day of this DateTime at the specified time zone.
      Returns:
      the LocalTime
    • toLocalTime

      @NotNull public LocalTime toLocalTime​(@NotNull ZoneId zone)
      Get a LocalTime representing the time of day of this DateTime at the specified time zone.
      Returns:
      the LocalTime
    • getDate

      @Deprecated @NotNull public Date getDate()
      Deprecated.
      use toZonedDateTime() instead.
      Convert this DateTime to a Java Date. This DateTime will be truncated to milliseconds.
      Returns:
      a Java Date representing this DateTime
    • getInstant

      @NotNull public Instant getInstant()
      Convert this DateTime to a Java Instant.
      Returns:
      a Java Instant representing this DateTime
    • equals

      public boolean equals​(Object that)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo​(DateTime dateTime)
      Specified by:
      compareTo in interface Comparable<DateTime>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      @NotNull public String toString​(@NotNull TimeZone timeZone)
      Convert this DateTime into a String using the provided TimeZone.

      The date will be formatted as yyyy-MM-DDThh:mm:ss.SSSSSSSSS TZ, for example 2020-05-27T13:37:57.780853000 NY or 2020-05-27T17:37:42.763641000 UTC.

      Parameters:
      timeZone - the timezone for formatting the string
      Returns:
      a String representation of this DateTime
    • toDateString

      @NotNull public String toDateString()
      Get the date represented by this DateTime in the default TimeZone.
      Returns:
      The date (yyyy-MM-dd) represented by this DateTime in the default TimeZone.
    • toDateString

      @NotNull public String toDateString​(@NotNull TimeZone timeZone)
      Get the date represented by this DateTime in the given TimeZone.
      Parameters:
      timeZone - a TimeZone
      Returns:
      The date (yyyy-MM-dd) represented by this DateTime in the given timeZone.
    • toDateString

      @NotNull public String toDateString​(@NotNull org.joda.time.DateTimeZone timeZone)
      Get the date represented by this DateTime in the given Joda DateTimeZone in ISO date format yyyy-mm.
      Parameters:
      timeZone - A joda DateTimeZone
      Returns:
      The date (yyyy-MM-dd) represented by this DateTime in the given timeZone
    • toDateString

      @NotNull public String toDateString​(@NotNull String zoneId)
      Get the date represented by this DateTime in the time zone specified by zoneId in ISO 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 given zoneId
    • toDateString

      @NotNull public String toDateString​(@NotNull ZoneId timeZone)
      Get the date represented by this DateTime in the given java ZoneId in ISO date format.
      Parameters:
      timeZone - A java time zone ID.
      Returns:
      The date (yyyy-MM-dd) represented by this DateTime in the given timeZone
    • writeExternal

      public void writeExternal​(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal​(ObjectInput in) throws IOException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException