Class Timestamps
protobuf/timestamp.proto
. All operations throw an
IllegalArgumentException
if the input(s) are not valid.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Timestamp
A constant holding theTimestamp
of epoch time,1970-01-01T00:00:00.000000000Z
.static final Timestamp
A constant holding the maximum validTimestamp
,9999-12-31T23:59:59.999999999Z
.static final Timestamp
A constant holding the minimum validTimestamp
,0001-01-01T00:00:00Z
. -
Method Summary
Modifier and TypeMethodDescriptionstatic Timestamp
Add a duration to a timestamp.static Duration
Calculate the difference between two timestamps.static Timestamp
checkValid
(Timestamp timestamp) Throws anIllegalArgumentException
if the givenTimestamp
is not valid.static Timestamp
checkValid
(Timestamp.Builder timestampBuilder) Builds the given builder and throws anIllegalArgumentException
if it is not valid.static Comparator
<Timestamp> Returns aComparator
forTimestamps
which sorts in increasing chronological order.static int
Compares two timestamps.static Timestamp
Create a Timestamp from aDate
.static Timestamp
fromMicros
(long microseconds) Create a Timestamp from the number of microseconds elapsed from the epoch.static Timestamp
fromMillis
(long milliseconds) Create a Timestamp from the number of milliseconds elapsed from the epoch.static Timestamp
fromNanos
(long nanoseconds) Create a Timestamp from the number of nanoseconds elapsed from the epoch.static Timestamp
fromSeconds
(long seconds) Create a Timestamp from the number of seconds elapsed from the epoch.static boolean
isValid
(long seconds, int nanos) Returns true if the given number of seconds and nanos is a validTimestamp
.static boolean
Returns true if the givenTimestamp
is valid.static Timestamp
now()
Create aTimestamp
using the best-available (in terms of precision) system clock.static Timestamp
Parse from RFC 3339 date string to Timestamp.static Timestamp
parseUnchecked
(String value) Parses a string in RFC 3339 format into aTimestamp
.static Timestamp
Subtract a duration from a timestamp.static long
Convert a Timestamp to the number of microseconds elapsed from the epoch.static long
Convert a Timestamp to the number of milliseconds elapsed from the epoch.static long
Convert a Timestamp to the number of nanoseconds elapsed from the epoch.static long
Convert a Timestamp to the number of seconds elapsed from the epoch.static String
Convert Timestamp to RFC 3339 date string format.
-
Field Details
-
Method Details
-
comparator
Returns aComparator
forTimestamps
which sorts in increasing chronological order. Nulls and invalidTimestamps
are not allowed (seeisValid(com.google.protobuf.Timestamp)
). The returned comparator is serializable. -
compare
Compares two timestamps. The value returned is identical to what would be returned by:Timestamps.comparator().compare(x, y)
.- Returns:
- the value
0
ifx == y
; a value less than0
ifx < y
; and a value greater than0
ifx > y
-
isValid
Returns true if the givenTimestamp
is valid. Theseconds
value must be in the range [-62,135,596,800, +253,402,300,799] (i.e., between 0001-01-01T00:00:00Z and 9999-12-31T23:59:59Z). Thenanos
value must be in the range [0, +999,999,999].Note: Negative second values with fractional seconds must still have non-negative nanos values that count forward in time.
-
isValid
public static boolean isValid(long seconds, int nanos) Returns true if the given number of seconds and nanos is a validTimestamp
. Theseconds
value must be in the range [-62,135,596,800, +253,402,300,799] (i.e., between 0001-01-01T00:00:00Z and 9999-12-31T23:59:59Z). Thenanos
value must be in the range [0, +999,999,999].Note: Negative second values with fractional seconds must still have non-negative nanos values that count forward in time.
-
checkValid
Throws anIllegalArgumentException
if the givenTimestamp
is not valid. -
checkValid
Builds the given builder and throws anIllegalArgumentException
if it is not valid. SeecheckValid(Timestamp)
.- Returns:
- A valid, built
Timestamp
.
-
toString
Convert Timestamp to RFC 3339 date string format. The output will always be Z-normalized and uses 0, 3, 6 or 9 fractional digits as required to represent the exact value. Note that Timestamp can only represent time from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. See https://www.ietf.org/rfc/rfc3339.txtExample of generated format: "1972-01-01T10:00:20.021Z"
- Returns:
- The string representation of the given timestamp.
- Throws:
IllegalArgumentException
- if the given timestamp is not in the valid range.
-
parse
Parse from RFC 3339 date string to Timestamp. This method accepts all outputs oftoString(Timestamp)
and it also accepts any fractional digits (or none) and any offset as long as they fit into nano-seconds precision.Example of accepted format: "1972-01-01T10:00:20.021-05:00"
- Returns:
- a Timestamp parsed from the string
- Throws:
ParseException
- if parsing fails
-
parseUnchecked
Parses a string in RFC 3339 format into aTimestamp
.Identical to
parse(String)
, but throws anIllegalArgumentException
instead of aParseException
if parsing fails.- Returns:
- a
Timestamp
parsed from the string - Throws:
IllegalArgumentException
- if parsing fails
-
now
Create aTimestamp
using the best-available (in terms of precision) system clock.Note: that while this API is convenient, it may harm the testability of your code, as you're unable to mock the current time. Instead, you may want to consider injecting a clock instance to read the current time.
-
fromSeconds
Create a Timestamp from the number of seconds elapsed from the epoch. -
toSeconds
Convert a Timestamp to the number of seconds elapsed from the epoch.The result will be rounded down to the nearest second. E.g., if the timestamp represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 second.
-
fromMillis
Create a Timestamp from the number of milliseconds elapsed from the epoch. -
fromDate
- Throws:
IllegalArgumentException
- if the year is before 1 CE or after 9999 CE
-
toMillis
Convert a Timestamp to the number of milliseconds elapsed from the epoch.The result will be rounded down to the nearest millisecond. For instance, if the timestamp represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 millisecond.
-
fromMicros
Create a Timestamp from the number of microseconds elapsed from the epoch. -
toMicros
Convert a Timestamp to the number of microseconds elapsed from the epoch.The result will be rounded down to the nearest microsecond. E.g., if the timestamp represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 microsecond.
-
fromNanos
Create a Timestamp from the number of nanoseconds elapsed from the epoch. -
toNanos
Convert a Timestamp to the number of nanoseconds elapsed from the epoch. -
between
Calculate the difference between two timestamps. -
add
Add a duration to a timestamp. -
subtract
Subtract a duration from a timestamp.
-