Class DateTime

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

@JsonpDeserializable public class DateTime extends 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 Details

  • Method Details

    • ofEpochMilli

      public static DateTime ofEpochMilli(long epochMilli, 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(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(String text, 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(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(Temporal instant, 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 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 Instant toInstant()
      Converts this object to an Instant.
      Throws:
      DateTimeParseException - if this object was created from a string which can not be parsed
    • toInstant

      public Instant toInstant(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:
      DateTimeParseException - if this object was created from a string which can not be parsed
    • toZonedDateTime

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

      public ZonedDateTime toZonedDateTime(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:
      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:
      DateTimeParseException - if this object was created from a string which can not be parsed
    • toEpochMilli

      public long toEpochMilli(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:
      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(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object