Top Description Fields Constructors Methods
com.google.protobuf.util

public final Class Timestamps

extends Object
Class Inheritance
Static Imports
com.google.common.math.IntMath.checkedAdd, .IntMath.checkedSubtract, .LongMath.checkedAdd, .LongMath.checkedMultiply, .LongMath.checkedSubtract

Utilities to help create/manipulate protobuf/timestamp.proto. All operations throw an IllegalArgumentException if the input(s) are not valid.

Field Summary

Modifier and TypeField and Description
private static final Comparator<Timestamp>
public static final Timestamp
EPOCH

A constant holding the Timestamp of epoch time, 1970-01-01T00:00:00.000000000Z.

public static final Timestamp
MAX_VALUE

A constant holding the maximum valid Timestamp, 9999-12-31T23:59:59.999999999Z.

pack-priv static final long
pack-priv static final long
public static final Timestamp
MIN_VALUE

A constant holding the minimum valid Timestamp, 0001-01-01T00:00:00Z.

pack-priv static final long
pack-priv static final long
pack-priv static final long
pack-priv static final long
pack-priv static final long
private static final ThreadLocal<SimpleDateFormat>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Timestamp
add(Timestamp start, Duration length)

Add a duration to a timestamp.

public static Duration
between(Timestamp from, Timestamp to)

Calculate the difference between two timestamps.

public static Timestamp
checkValid(Timestamp timestamp)

Throws an IllegalArgumentException if the given Timestamp is not valid.

public static Timestamp

Returns:

A valid, built Timestamp.
checkValid
(Timestamp.Builder timestampBuilder)

Builds the given builder and throws an IllegalArgumentException if it is not valid.

public static Comparator<Timestamp>
comparator()

Returns a Comparator for Timestamps which sorts in increasing chronological order.

public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
compare
(Timestamp x, Timestamp y)

Compares two timestamps.

private static SimpleDateFormat
pack-priv static String
formatNanos(int nanos)

Format the nano part of a timestamp or a duration.

public static Timestamp
fromMicros(long microseconds)

Create a Timestamp from the number of microseconds elapsed from the epoch.

public static Timestamp
fromMillis(long milliseconds)

Create a Timestamp from the number of milliseconds elapsed from the epoch.

public static Timestamp
fromNanos(long nanoseconds)

Create a Timestamp from the number of nanoseconds elapsed from the epoch.

public static Timestamp
fromSeconds(long seconds)

Create a Timestamp from the number of seconds elapsed from the epoch.

public static boolean
isValid(Timestamp timestamp)

Returns true if the given Timestamp is valid.

public static boolean
isValid(long seconds, int nanos)

Returns true if the given number of seconds and nanos is a valid Timestamp.

pack-priv static Timestamp
normalizedTimestamp(long seconds, int nanos)

public static Timestamp

Returns:

A Timestamp parsed from the string.
parse
(String value)

Parse from RFC 3339 date string to Timestamp.

pack-priv static int
private static long
public static Timestamp
subtract(Timestamp start, Duration length)

Subtract a duration from a timestamp.

public static long
toMicros(Timestamp timestamp)

Convert a Timestamp to the number of microseconds elapsed from the epoch.

public static long
toMillis(Timestamp timestamp)

Convert a Timestamp to the number of milliseconds elapsed from the epoch.

public static long
toNanos(Timestamp timestamp)

Convert a Timestamp to the number of nanoseconds elapsed from the epoch.

public static long
toSeconds(Timestamp timestamp)

Convert a Timestamp to the number of seconds elapsed from the epoch.

public static String

Returns:

The string representation of the given timestamp.
toString
(Timestamp timestamp)

Convert Timestamp to RFC 3339 date string format.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COMPARATORback to summary
private static final Comparator<Timestamp> COMPARATOR
EPOCHback to summary
public static final Timestamp EPOCH

A constant holding the Timestamp of epoch time, 1970-01-01T00:00:00.000000000Z.

MAX_VALUEback to summary
public static final Timestamp MAX_VALUE

A constant holding the maximum valid Timestamp, 9999-12-31T23:59:59.999999999Z.

MICROS_PER_SECONDback to summary
pack-priv static final long MICROS_PER_SECOND
MILLIS_PER_SECONDback to summary
pack-priv static final long MILLIS_PER_SECOND
MIN_VALUEback to summary
public static final Timestamp MIN_VALUE

A constant holding the minimum valid Timestamp, 0001-01-01T00:00:00Z.

NANOS_PER_MICROSECONDback to summary
pack-priv static final long NANOS_PER_MICROSECOND
NANOS_PER_MILLISECONDback to summary
pack-priv static final long NANOS_PER_MILLISECOND
NANOS_PER_SECONDback to summary
pack-priv static final long NANOS_PER_SECOND
TIMESTAMP_SECONDS_MAXback to summary
pack-priv static final long TIMESTAMP_SECONDS_MAX
TIMESTAMP_SECONDS_MINback to summary
pack-priv static final long TIMESTAMP_SECONDS_MIN
timestampFormatback to summary
private static final ThreadLocal<SimpleDateFormat> timestampFormat

Constructor Detail

Timestampsback to summary
private Timestamps()

Method Detail

addback to summary
public static Timestamp add(Timestamp start, Duration length)

Add a duration to a timestamp.

betweenback to summary
public static Duration between(Timestamp from, Timestamp to)

Calculate the difference between two timestamps.

checkValidback to summary
public static Timestamp checkValid(Timestamp timestamp)

Throws an IllegalArgumentException if the given Timestamp is not valid.

Annotations
@CanIgnoreReturnValue
checkValidback to summary
public static Timestamp checkValid(Timestamp.Builder timestampBuilder)

Builds the given builder and throws an IllegalArgumentException if it is not valid. See checkValid(Timestamp).

Returns:Timestamp

A valid, built Timestamp.

comparatorback to summary
public static Comparator<Timestamp> comparator()

Returns a Comparator for Timestamps which sorts in increasing chronological order. Nulls and invalid Timestamps are not allowed (see isValid).

compareback to summary
public static int compare(Timestamp x, Timestamp y)

Compares two timestamps. The value returned is identical to what would be returned by: Timestamps.comparator().compare(x, y).

Returns:int

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y

createTimestampFormatback to summary
private static SimpleDateFormat createTimestampFormat()
formatNanosback to summary
pack-priv static String formatNanos(int nanos)

Format the nano part of a timestamp or a duration.

fromMicrosback to summary
public static Timestamp fromMicros(long microseconds)

Create a Timestamp from the number of microseconds elapsed from the epoch.

Annotations
@SuppressWarnings:GoodTime
fromMillisback to summary
public static Timestamp fromMillis(long milliseconds)

Create a Timestamp from the number of milliseconds elapsed from the epoch.

Annotations
@SuppressWarnings:GoodTime
fromNanosback to summary
public static Timestamp fromNanos(long nanoseconds)

Create a Timestamp from the number of nanoseconds elapsed from the epoch.

Annotations
@SuppressWarnings:GoodTime
fromSecondsback to summary
public static Timestamp fromSeconds(long seconds)

Create a Timestamp from the number of seconds elapsed from the epoch.

Annotations
@SuppressWarnings:GoodTime
isValidback to summary
public static boolean isValid(Timestamp timestamp)

Returns true if the given Timestamp is valid. The seconds 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). The nanos 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.

isValidback to summary
public static boolean isValid(long seconds, int nanos)

Returns true if the given number of seconds and nanos is a valid Timestamp. The seconds 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). The nanos 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.

Annotations
@SuppressWarnings:GoodTime
normalizedTimestampback to summary
pack-priv static Timestamp normalizedTimestamp(long seconds, int nanos)
parseback to summary
public static Timestamp parse(String value) throws ParseException

Parse from RFC 3339 date string to Timestamp. This method accepts all outputs of toString(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:Timestamp

A Timestamp parsed from the string.

Exceptions
ParseException:
if parsing fails.
parseNanosback to summary
pack-priv static int parseNanos(String value) throws ParseException
parseTimezoneOffsetback to summary
private static long parseTimezoneOffset(String value) throws ParseException
subtractback to summary
public static Timestamp subtract(Timestamp start, Duration length)

Subtract a duration from a timestamp.

toMicrosback to summary
public static long toMicros(Timestamp timestamp)

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.

Annotations
@SuppressWarnings:GoodTime
toMillisback to summary
public static long toMillis(Timestamp timestamp)

Convert a Timestamp to the number of milliseconds elapsed from the epoch.

The result will be rounded down to the nearest millisecond. E.g., if the timestamp represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 millisecond.

Annotations
@SuppressWarnings:GoodTime
toNanosback to summary
public static long toNanos(Timestamp timestamp)

Convert a Timestamp to the number of nanoseconds elapsed from the epoch.

Annotations
@SuppressWarnings:GoodTime
toSecondsback to summary
public static long toSeconds(Timestamp timestamp)

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.

Annotations
@SuppressWarnings:GoodTime
toStringback to summary
public static String toString(Timestamp timestamp)

Convert Timestamp to RFC 3339 date string format. The output will always be Z-normalized and uses 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.txt

Example of generated format: "1972-01-01T10:00:20.021Z"

Returns:String

The string representation of the given timestamp.

Exceptions
IllegalArgumentException:
if the given timestamp is not in the valid range.