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

public final Class Durations

extends Object
Class Inheritance
Static Imports
com.google.common.base.Preconditions.checkArgument, com.google.common.math.IntMath.checkedAdd, .IntMath.checkedSubtract, .LongMath.checkedAdd, .LongMath.checkedMultiply, .LongMath.checkedSubtract, com.google.protobuf.util.Timestamps.MICROS_PER_SECOND, .Timestamps.MILLIS_PER_SECOND, .Timestamps.NANOS_PER_MICROSECOND, .Timestamps.NANOS_PER_MILLISECOND, .Timestamps.NANOS_PER_SECOND

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

Field Summary

Modifier and TypeField and Description
private static final Comparator<Duration>
pack-priv static final long
pack-priv static final long
public static final Duration
MAX_VALUE

A constant holding the maximum valid Duration, approximately +10,000 years.

public static final Duration
MIN_VALUE

A constant holding the minimum valid Duration, approximately -10,000 years.

private static final long
private static final long
private static final long
public static final Duration
ZERO

A constant holding the duration of zero.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Duration
add(Duration d1, Duration d2)

Add two durations.

public static Duration
checkNotNegative(Duration duration)

Ensures that the given Duration is not negative.

public static Duration
checkPositive(Duration duration)

Ensures that the given Duration is positive.

public static Duration
checkValid(Duration duration)

Throws an IllegalArgumentException if the given Duration is not valid.

public static Duration

Returns:

A valid, built Duration.
checkValid
(Duration.Builder durationBuilder)

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

public static Comparator<Duration>
comparator()

Returns a Comparator for Durations 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
(Duration x, Duration y)

Compares two durations.

public static Duration
fromDays(long days)

Create a Duration from the number of days.

public static Duration
fromHours(long hours)

Create a Duration from the number of hours.

public static Duration
fromMicros(long microseconds)

Create a Duration from the number of microseconds.

public static Duration
fromMillis(long milliseconds)

Create a Duration from the number of milliseconds.

public static Duration
fromMinutes(long minutes)

Create a Duration from the number of minutes.

public static Duration
fromNanos(long nanoseconds)

Create a Duration from the number of nanoseconds.

public static Duration
fromSeconds(long seconds)

Create a Duration from the number of seconds.

public static boolean
isNegative(Duration duration)

Returns whether the given Duration is negative or not.

public static boolean
isValid(Duration duration)

Returns true if the given Duration is valid.

public static boolean
isValid(long seconds, int nanos)

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

pack-priv static Duration
normalizedDuration(long seconds, int nanos)

public static Duration

Returns:

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

Parse from a string to produce a duration.

public static Duration
subtract(Duration d1, Duration d2)

Subtract a duration from another.

public static long
toDays(Duration duration)

Convert a Duration to the number of days.

public static long
toHours(Duration duration)

Convert a Duration to the number of hours.

public static long
toMicros(Duration duration)

Convert a Duration to the number of microseconds.

public static long
toMillis(Duration duration)

Convert a Duration to the number of milliseconds.

public static long
toMinutes(Duration duration)

Convert a Duration to the number of minutes.

public static long
toNanos(Duration duration)

Convert a Duration to the number of nanoseconds.

public static long
toSeconds(Duration duration)

Convert a Duration to the number of seconds.

public static double
toSecondsAsDouble(Duration duration)

Returns the number of seconds of the given duration as a double.

public static String

Returns:

The string representation of the given duration.
toString
(Duration duration)

Convert Duration to string format.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COMPARATORback to summary
private static final Comparator<Duration> COMPARATOR
DURATION_SECONDS_MAXback to summary
pack-priv static final long DURATION_SECONDS_MAX
DURATION_SECONDS_MINback to summary
pack-priv static final long DURATION_SECONDS_MIN
MAX_VALUEback to summary
public static final Duration MAX_VALUE

A constant holding the maximum valid Duration, approximately +10,000 years.

MIN_VALUEback to summary
public static final Duration MIN_VALUE

A constant holding the minimum valid Duration, approximately -10,000 years.

SECONDS_PER_DAYback to summary
private static final long SECONDS_PER_DAY
SECONDS_PER_HOURback to summary
private static final long SECONDS_PER_HOUR
SECONDS_PER_MINUTEback to summary
private static final long SECONDS_PER_MINUTE
ZEROback to summary
public static final Duration ZERO

A constant holding the duration of zero.

Constructor Detail

Durationsback to summary
private Durations()

Method Detail

addback to summary
public static Duration add(Duration d1, Duration d2)

Add two durations.

checkNotNegativeback to summary
public static Duration checkNotNegative(Duration duration)

Ensures that the given Duration is not negative.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if duration is negative or invalid
NullPointerException:
if duration is null
checkPositiveback to summary
public static Duration checkPositive(Duration duration)

Ensures that the given Duration is positive.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if duration is negative, ZERO, or invalid
NullPointerException:
if duration is null
checkValidback to summary
public static Duration checkValid(Duration duration)

Throws an IllegalArgumentException if the given Duration is not valid.

Annotations
@CanIgnoreReturnValue
checkValidback to summary
public static Duration checkValid(Duration.Builder durationBuilder)

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

Returns:Duration

A valid, built Duration.

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

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

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

Compares two durations. The value returned is identical to what would be returned by: Durations.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

fromDaysback to summary
public static Duration fromDays(long days)

Create a Duration from the number of days.

Annotations
@SuppressWarnings:GoodTime
fromHoursback to summary
public static Duration fromHours(long hours)

Create a Duration from the number of hours.

Annotations
@SuppressWarnings:GoodTime
fromMicrosback to summary
public static Duration fromMicros(long microseconds)

Create a Duration from the number of microseconds.

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

Create a Duration from the number of milliseconds.

Annotations
@SuppressWarnings:GoodTime
fromMinutesback to summary
public static Duration fromMinutes(long minutes)

Create a Duration from the number of minutes.

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

Create a Duration from the number of nanoseconds.

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

Create a Duration from the number of seconds.

Annotations
@SuppressWarnings:GoodTime
isNegativeback to summary
public static boolean isNegative(Duration duration)

Returns whether the given Duration is negative or not.

isValidback to summary
public static boolean isValid(Duration duration)

Returns true if the given Duration is valid. The seconds value must be in the range [-315,576,000,000, +315,576,000,000]. The nanos value must be in the range [-999,999,999, +999,999,999].

Note

Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field.

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

Returns true if the given number of seconds and nanos is a valid Duration. The seconds value must be in the range [-315,576,000,000, +315,576,000,000]. The nanos value must be in the range [-999,999,999, +999,999,999].

Note

Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field.

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

Parse from a string to produce a duration.

Returns:Duration

A Duration parsed from the string.

Exceptions
ParseException:
if parsing fails.
subtractback to summary
public static Duration subtract(Duration d1, Duration d2)

Subtract a duration from another.

toDaysback to summary
public static long toDays(Duration duration)

Convert a Duration to the number of days. The result will be rounded towards 0 to the nearest day.

Annotations
@SuppressWarnings:GoodTime
toHoursback to summary
public static long toHours(Duration duration)

Convert a Duration to the number of hours. The result will be rounded towards 0 to the nearest hour.

Annotations
@SuppressWarnings:GoodTime
toMicrosback to summary
public static long toMicros(Duration duration)

Convert a Duration to the number of microseconds. The result will be rounded towards 0 to the nearest microseconds. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.

Annotations
@SuppressWarnings:GoodTime
toMillisback to summary
public static long toMillis(Duration duration)

Convert a Duration to the number of milliseconds. The result will be rounded towards 0 to the nearest millisecond. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.

Annotations
@SuppressWarnings:GoodTime
toMinutesback to summary
public static long toMinutes(Duration duration)

Convert a Duration to the number of minutes. The result will be rounded towards 0 to the nearest minute.

Annotations
@SuppressWarnings:GoodTime
toNanosback to summary
public static long toNanos(Duration duration)

Convert a Duration to the number of nanoseconds.

Annotations
@SuppressWarnings:GoodTime
toSecondsback to summary
public static long toSeconds(Duration duration)

Convert a Duration to the number of seconds. The result will be rounded towards 0 to the nearest second. E.g., if the duration represents -1 nanosecond, it will be rounded to 0.

Annotations
@SuppressWarnings:GoodTime
toSecondsAsDoubleback to summary
public static double toSecondsAsDouble(Duration duration)

Returns the number of seconds of the given duration as a double. This method should be used to accommodate APIs that only accept durations as double values.

This conversion may lose precision.

If you need the number of seconds in this duration as a long (not a double), simply use duration.getSeconds() or toSeconds (which includes validation).

Annotations
@SuppressWarnings:DurationSecondsToDouble, GoodTime
toStringback to summary
public static String toString(Duration duration)

Convert Duration to string format. The string format will contains 3, 6, or 9 fractional digits depending on the precision required to represent the exact Duration value. For example: "1s", "1.010s", "1.000000100s", "-3.100s" The range that can be represented by Duration is from -315,576,000,000 to +315,576,000,000 inclusive (in seconds).

Returns:String

The string representation of the given duration.

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