Class Timestamp

java.lang.Object
com.google.cloud.Timestamp
All Implemented Interfaces:
Serializable, Comparable<Timestamp>

public final class Timestamp extends Object implements Comparable<Timestamp>, Serializable
Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01, 9999-12-31].

Timestamp instances are immutable.

See Also:
  • Field Details

    • MIN_VALUE

      public static final Timestamp MIN_VALUE
      The smallest legal timestamp ("0001-01-01T00:00:00Z").
    • MAX_VALUE

      public static final Timestamp MAX_VALUE
      The largest legal timestamp ("9999-12-31T23:59:59Z").
  • Method Details

    • ofTimeSecondsAndNanos

      public static Timestamp ofTimeSecondsAndNanos(long seconds, int nanos)
      Creates an instance representing the value of seconds and nanos since January 1, 1970, 00:00:00 UTC.
      Parameters:
      seconds - seconds since January 1, 1970, 00:00:00 UTC. A negative value is the number of seconds before January 1, 1970, 00:00:00 UTC.
      nanos - the fractional seconds component, in the range 0..999999999.
      Throws:
      IllegalArgumentException - if the timestamp is outside the representable range
    • ofTimeMicroseconds

      public static Timestamp ofTimeMicroseconds(long microseconds)
      Creates an instance representing the value of microseconds.
      Throws:
      IllegalArgumentException - if the timestamp is outside the representable range
    • of

      public static Timestamp of(Date date)
      Creates an instance representing the value of Date.
      Throws:
      IllegalArgumentException - if the timestamp is outside the representable range
    • now

      public static Timestamp now()
      Creates an instance with current time.
    • of

      public static Timestamp of(Timestamp timestamp)
      Creates an instance representing the value of timestamp.
      Throws:
      IllegalArgumentException - if the timestamp is outside the representable range
    • getSeconds

      public long getSeconds()
      Returns the number of seconds since January 1, 1970, 00:00:00 UTC. A negative value is the number of seconds before January 1, 1970, 00:00:00 UTC.
    • getNanos

      public int getNanos()
      Returns the fractional seconds component, in nanoseconds.
    • toSqlTimestamp

      public Timestamp toSqlTimestamp()
      Returns a JDBC timestamp initialized to the same point in time as this.
    • toDate

      public Date toDate()
      Returns a new java.util.Date corresponding to this timestamp. Any sub-millisecond precision will be stripped.
      Returns:
      An approximate java.util.Date representation of this timestamp.
    • compareTo

      public int compareTo(Timestamp other)
      Specified by:
      compareTo in interface Comparable<Timestamp>
    • fromProto

      public static Timestamp fromProto(com.google.protobuf.Timestamp proto)
      Creates an instance of Timestamp from com.google.protobuf.Timestamp.
    • toProto

      public com.google.protobuf.Timestamp toProto()
      Returns a com.google.protobuf.Timestamp initialized to the same point in time as this.
    • parseTimestamp

      @ObsoleteApi("Use parseTimestampDuration(String) instead") public static Timestamp parseTimestamp(String timestamp)
      This method is obsolete. Use parseTimestampDuration(String) instead
    • parseTimestampDuration

      public static Timestamp parseTimestampDuration(String timestamp)
      Creates a Timestamp instance from the given string. Input string should be in the RFC 3339 format, like '2020-12-01T10:15:30.000Z' or with the timezone offset, such as '2020-12-01T10:15:30+01:00'.
      Parameters:
      timestamp - string in the RFC 3339 format
      Returns:
      created Timestamp
      Throws:
      DateTimeParseException - if unable to parse
    • toString

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

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

      public int hashCode()
      Overrides:
      hashCode in class Object