public final class Durations
extends java.lang.Object
protobuf/duration.proto
. All operations throw an
IllegalArgumentException
if the input(s) are not valid.Modifier and Type | Field and Description |
---|---|
static Duration |
MAX_VALUE
A constant holding the maximum valid
Duration , approximately +10,000 years. |
static Duration |
MIN_VALUE
A constant holding the minimum valid
Duration , approximately -10,000 years. |
static Duration |
ZERO
A constant holding the duration of zero.
|
Modifier and Type | Method and Description |
---|---|
static Duration |
add(Duration d1,
Duration d2)
Add two durations.
|
static Duration |
checkNotNegative(Duration duration)
Ensures that the given
Duration is not negative. |
static Duration |
checkPositive(Duration duration)
Ensures that the given
Duration is positive. |
static Duration |
checkValid(Duration.Builder durationBuilder)
Builds the given builder and throws an
IllegalArgumentException if it is not valid. |
static Duration |
checkValid(Duration duration)
Throws an
IllegalArgumentException if the given Duration is not valid. |
static java.util.Comparator<Duration> |
comparator()
Returns a
Comparator for Duration s which sorts in increasing chronological
order. |
static int |
compare(Duration x,
Duration y)
Compares two durations.
|
static Duration |
fromDays(long days)
Create a Duration from the number of days.
|
static Duration |
fromHours(long hours)
Create a Duration from the number of hours.
|
static Duration |
fromMicros(long microseconds)
Create a Duration from the number of microseconds.
|
static Duration |
fromMillis(long milliseconds)
Create a Duration from the number of milliseconds.
|
static Duration |
fromMinutes(long minutes)
Create a Duration from the number of minutes.
|
static Duration |
fromNanos(long nanoseconds)
Create a Duration from the number of nanoseconds.
|
static Duration |
fromSeconds(long seconds)
Create a Duration from the number of seconds.
|
static boolean |
isNegative(Duration duration)
Returns whether the given
Duration is negative or not. |
static boolean |
isPositive(Duration duration)
Returns whether the given
Duration is positive or not. |
static boolean |
isValid(Duration duration)
Returns true if the given
Duration is valid. |
static boolean |
isValid(long seconds,
int nanos)
Returns true if the given number of seconds and nanos is a valid
Duration . |
static Duration |
parse(java.lang.String value)
Parse from a string to produce a duration.
|
static Duration |
subtract(Duration d1,
Duration d2)
Subtract a duration from another.
|
static long |
toDays(Duration duration)
Convert a Duration to the number of days.
|
static long |
toHours(Duration duration)
Convert a Duration to the number of hours.
|
static long |
toMicros(Duration duration)
Convert a Duration to the number of microseconds.
|
static long |
toMillis(Duration duration)
Convert a Duration to the number of milliseconds.
|
static long |
toMinutes(Duration duration)
Convert a Duration to the number of minutes.
|
static long |
toNanos(Duration duration)
Convert a Duration to the number of nanoseconds.
|
static long |
toSeconds(Duration duration)
Convert a Duration to the number of seconds.
|
static double |
toSecondsAsDouble(Duration duration)
Returns the number of seconds of the given duration as a
double . |
static java.lang.String |
toString(Duration duration)
Convert Duration to string format.
|
public static final Duration MIN_VALUE
Duration
, approximately -10,000
years.public static final Duration MAX_VALUE
Duration
, approximately +10,000
years.public static final Duration ZERO
public static java.util.Comparator<Duration> comparator()
Comparator
for Duration
s which sorts in increasing chronological
order. Nulls and invalid Duration
s are not allowed (see isValid(com.google.protobuf.Duration)
). The returned
comparator is serializable.public static int compare(Duration x, Duration y)
Durations.comparator().compare(x, y)
.0
if x == y
; a value less than 0
if x < y
;
and a value greater than 0
if x > y
public static boolean isValid(Duration duration)
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.
public static boolean isValid(long seconds, int nanos)
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.
public static boolean isNegative(Duration duration)
Duration
is negative or not.public static boolean isPositive(Duration duration)
Duration
is positive or not.@CanIgnoreReturnValue public static Duration checkNotNegative(Duration duration)
Duration
is not negative.java.lang.IllegalArgumentException
- if duration
is negative or invalidjava.lang.NullPointerException
- if duration
is null
@CanIgnoreReturnValue public static Duration checkPositive(Duration duration)
Duration
is positive.java.lang.IllegalArgumentException
- if duration
is negative, ZERO
, or invalidjava.lang.NullPointerException
- if duration
is null
@CanIgnoreReturnValue public static Duration checkValid(Duration duration)
IllegalArgumentException
if the given Duration
is not valid.public static Duration checkValid(Duration.Builder durationBuilder)
IllegalArgumentException
if it is not valid. See
checkValid(Duration)
.Duration
.public static java.lang.String toString(Duration duration)
java.lang.IllegalArgumentException
- if the given duration is not in the valid range.public static Duration parse(java.lang.String value) throws java.text.ParseException
java.text.ParseException
- if parsing fails.public static Duration fromDays(long days)
public static Duration fromHours(long hours)
public static Duration fromMinutes(long minutes)
public static Duration fromSeconds(long seconds)
public static Duration fromMillis(long milliseconds)
public static Duration fromMicros(long microseconds)
public static Duration fromNanos(long nanoseconds)
public static long toDays(Duration duration)
public static long toHours(Duration duration)
public static long toMinutes(Duration duration)
public static long toSeconds(Duration duration)
public static double toSecondsAsDouble(Duration duration)
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(com.google.protobuf.Duration)
(which includes validation).
public static long toMillis(Duration duration)
public static long toMicros(Duration duration)
public static long toNanos(Duration duration)