Class DateTime

java.lang.Object
co.elastic.clients.util.DateTime
All Implemented Interfaces:
JsonpSerializable

@JsonpDeserializable
public class DateTime
extends java.lang.Object
implements JsonpSerializable
A date-time that can be represented either as a string or a number of milliseconds since the Epoch.

The string for can also be used to represent date-math expressions where applicable.

  • Field Summary

    Fields
    Modifier and Type Field Description
    static JsonpDeserializer<DateTime> _DESERIALIZER  
  • Method Summary

    Modifier and Type Method Description
    boolean equals​(java.lang.Object o)  
    java.lang.String getString()
    Get the string that was used to create this date-time, or null if it was created with a number of milliseconds.
    int hashCode()  
    static DateTime of​(java.lang.String text)
    Create a new date-time or a date-math expression from a string.
    static DateTime of​(java.lang.String text, java.time.format.DateTimeFormatter parser)
    Create a new date-time from a string and parser.
    static DateTime of​(java.time.temporal.Temporal instant)
    Create a new date-time from a Temporal object.
    static DateTime of​(java.time.temporal.Temporal instant, java.time.format.DateTimeFormatter format)
    Create a new date-time from a Temporal object.
    static DateTime ofEpochMilli​(long epochMilli)
    Create a new date-time from Epoch milliseconds.
    static DateTime ofEpochMilli​(long epochMilli, java.time.format.DateTimeFormatter format)
    Create a new date-time from Epoch milliseconds.
    void serialize​(jakarta.json.stream.JsonGenerator generator, JsonpMapper mapper)  
    long toEpochMilli()
    Get the milliseconds since the Epoch for represented by this object.
    long toEpochMilli​(java.time.format.DateTimeFormatter format)
    Get the milliseconds since the Epoch for represented by this object.
    java.time.Instant toInstant()
    Converts this object to an Instant.
    java.time.Instant toInstant​(java.time.format.DateTimeFormatter parser)
    Converts this object to an Instant, using a given format.
    java.lang.String toString()  
    java.time.ZonedDateTime toZonedDateTime()
    Converts this object to an ZonedDateTime.
    java.time.ZonedDateTime toZonedDateTime​(java.time.format.DateTimeFormatter parser)
    Converts this object to an ZonedDateTime, using a given format.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Method Details

    • ofEpochMilli

      public static DateTime ofEpochMilli​(long epochMilli, java.time.format.DateTimeFormatter format)
      Create a new date-time from Epoch milliseconds. It will be serialized to JSON as a string.
      Parameters:
      epochMilli - the timestamp
      format - the format used to format the date-time as a JSON string
    • ofEpochMilli

      public static DateTime ofEpochMilli​(long epochMilli)
      Create a new date-time from Epoch milliseconds. It will be serialized to JSON as a number.
      Parameters:
      epochMilli - the timestamp
    • of

      public static DateTime of​(java.lang.String text)
      Create a new date-time or a date-math expression from a string. It will be serialized to JSON as is, conversion to an Instant will use the default parsers.
      Parameters:
      text - the date-time as a string, or a date-math expression. If a date-math expression, the result will fail to be converted to another type.
    • of

      public static DateTime of​(java.lang.String text, java.time.format.DateTimeFormatter parser)
      Create a new date-time from a string and parser. It will be serialized to JSON as is.
      Parameters:
      text - the date-time as a string
      parser - the parser used to convert the date-time to an Instant
    • of

      public static DateTime of​(java.time.temporal.Temporal instant)
      Create a new date-time from a Temporal object. It will be serialized to JSON as milliseconds since the Epoch.
      Parameters:
      instant - the date-time
    • of

      public static DateTime of​(java.time.temporal.Temporal instant, java.time.format.DateTimeFormatter format)
      Create a new date-time from a Temporal object. It will be serialized to JSON as a string.
      Parameters:
      instant - the date-time
      format - the format used to convert to a JSON string
    • getString

      @Nullable public java.lang.String getString()
      Get the string that was used to create this date-time, or null if it was created with a number of milliseconds.
    • toInstant

      public java.time.Instant toInstant()
      Converts this object to an Instant.
      Throws:
      java.time.format.DateTimeParseException - if this object was created from a string which can not be parsed
    • toInstant

      public java.time.Instant toInstant​(java.time.format.DateTimeFormatter parser)
      Converts this object to an Instant, using a given format.
      Parameters:
      parser - the parser to use if this object was created from a string
      Throws:
      java.time.format.DateTimeParseException - if this object was created from a string which can not be parsed
    • toZonedDateTime

      public java.time.ZonedDateTime toZonedDateTime()
      Converts this object to an ZonedDateTime.
      Throws:
      java.time.format.DateTimeParseException - if this object was created from a string which can not be parsed
    • toZonedDateTime

      public java.time.ZonedDateTime toZonedDateTime​(java.time.format.DateTimeFormatter parser)
      Converts this object to an ZonedDateTime, using a given format.
      Parameters:
      parser - the parser to use if this object was created from a string
      Throws:
      java.time.format.DateTimeParseException - if this object was created from a string which can not be parsed
    • toEpochMilli

      public long toEpochMilli()
      Get the milliseconds since the Epoch for represented by this object. If it was created from a string, it is first converted using toInstant().
      Throws:
      java.time.format.DateTimeParseException - if this object was created from a string which can not be parsed
    • toEpochMilli

      public long toEpochMilli​(java.time.format.DateTimeFormatter format)
      Get the milliseconds since the Epoch for represented by this object. If it was created from a string, it is first converted using toInstant(DateTimeFormatter).
      Parameters:
      format - the parser to use if this object was created from a string
      Throws:
      java.time.format.DateTimeParseException - if this object was created from a string which can not be parsed
    • serialize

      public void serialize​(jakarta.json.stream.JsonGenerator generator, JsonpMapper mapper)
      Specified by:
      serialize in interface JsonpSerializable
    • equals

      public boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object