Class Durations
protobuf/duration.proto. All operations throw an
IllegalArgumentException if the input(s) are not valid.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final long(package private) static final longstatic final DurationA constant holding the maximum validDuration, approximately+10,000years.static final DurationA constant holding the minimum validDuration, approximately-10,000years.private static final longprivate static final longprivate static final longstatic final DurationA constant holding the duration of zero. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DurationAdd two durations.static DurationcheckNotNegative(Duration duration) Ensures that the givenDurationis not negative.static DurationcheckPositive(Duration duration) Ensures that the givenDurationis positive.static DurationcheckValid(Duration duration) Throws anIllegalArgumentExceptionif the givenDurationis not valid.static DurationcheckValid(Duration.Builder durationBuilder) Builds the given builder and throws anIllegalArgumentExceptionif it is not valid.static Comparator<Duration>Returns aComparatorforDurations which sorts in increasing chronological order.static intCompares two durations.static DurationfromDays(long days) Create a Duration from the number of days.static DurationfromHours(long hours) Create a Duration from the number of hours.static DurationfromMicros(long microseconds) Create a Duration from the number of microseconds.static DurationfromMillis(long milliseconds) Create a Duration from the number of milliseconds.static DurationfromMinutes(long minutes) Create a Duration from the number of minutes.static DurationfromNanos(long nanoseconds) Create a Duration from the number of nanoseconds.static DurationfromSeconds(long seconds) Create a Duration from the number of seconds.static booleanisNegative(Duration duration) Returns whether the givenDurationis negative or not.static booleanisPositive(Duration duration) Returns whether the givenDurationis positive or not.static booleanisValid(long seconds, int nanos) Returns true if the given number of seconds and nanos is a validDuration.static booleanReturns true if the givenDurationis valid.(package private) static DurationnormalizedDuration(long seconds, int nanos) static DurationParse a string to produce a duration.static DurationparseUnchecked(String value) Parses a string in RFC 3339 format into aDuration.static DurationSubtract a duration from another.static longConvert a Duration to the number of days.static longConvert a Duration to the number of hours.static longConvert a Duration to the number of microseconds.static longConvert a Duration to the number of milliseconds.static longConvert a Duration to the number of minutes.static longConvert a Duration to the number of nanoseconds.static longConvert a Duration to the number of seconds.static doubletoSecondsAsDouble(Duration duration) Returns the number of seconds of the given duration as adouble.static StringConvert Duration to string format.
-
Field Details
-
DURATION_SECONDS_MIN
static final long DURATION_SECONDS_MIN- See Also:
-
DURATION_SECONDS_MAX
static final long DURATION_SECONDS_MAX- See Also:
-
SECONDS_PER_MINUTE
private static final long SECONDS_PER_MINUTE- See Also:
-
SECONDS_PER_HOUR
private static final long SECONDS_PER_HOUR- See Also:
-
SECONDS_PER_DAY
private static final long SECONDS_PER_DAY- See Also:
-
MIN_VALUE
A constant holding the minimum validDuration, approximately-10,000years. -
MAX_VALUE
A constant holding the maximum validDuration, approximately+10,000years. -
ZERO
A constant holding the duration of zero.
-
-
Constructor Details
-
Durations
private Durations()
-
-
Method Details
-
comparator
Returns aComparatorforDurations which sorts in increasing chronological order. Nulls and invalidDurations are not allowed (seeisValid(com.google.protobuf.Duration)). The returned comparator is serializable. -
compare
Compares two durations. The value returned is identical to what would be returned by:Durations.comparator().compare(x, y).- Returns:
- the value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y
-
isValid
Returns true if the givenDurationis valid. Thesecondsvalue must be in the range [-315,576,000,000, +315,576,000,000]. Thenanosvalue must be in the range [-999,999,999, +999,999,999].Note: Durations less than one second are represented with a 0
secondsfield and a positive or negativenanosfield. For durations of one second or more, a non-zero value for thenanosfield must be of the same sign as thesecondsfield. -
isValid
public static boolean isValid(long seconds, int nanos) Returns true if the given number of seconds and nanos is a validDuration. Thesecondsvalue must be in the range [-315,576,000,000, +315,576,000,000]. Thenanosvalue must be in the range [-999,999,999, +999,999,999].Note: Durations less than one second are represented with a 0
secondsfield and a positive or negativenanosfield. For durations of one second or more, a non-zero value for thenanosfield must be of the same sign as thesecondsfield. -
isNegative
Returns whether the givenDurationis negative or not. -
isPositive
Returns whether the givenDurationis positive or not. -
checkNotNegative
Ensures that the givenDurationis not negative.- Throws:
IllegalArgumentException- ifdurationis negative or invalidNullPointerException- ifdurationisnull
-
checkPositive
Ensures that the givenDurationis positive.- Throws:
IllegalArgumentException- ifdurationis negative,ZERO, or invalidNullPointerException- ifdurationisnull
-
checkValid
Throws anIllegalArgumentExceptionif the givenDurationis not valid. -
checkValid
Builds the given builder and throws anIllegalArgumentExceptionif it is not valid. SeecheckValid(Duration).- Returns:
- A valid, built
Duration.
-
toString
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:
- The string representation of the given duration.
- Throws:
IllegalArgumentException- if the given duration is not in the valid range.
-
parse
Parse a string to produce a duration.- Returns:
- a Duration parsed from the string
- Throws:
ParseException- if the string is not in the duration format
-
parseUnchecked
Parses a string in RFC 3339 format into aDuration.Identical to
parse(String), but throws anIllegalArgumentExceptioninstead of aParseExceptionif parsing fails.- Returns:
- a
Durationparsed from the string - Throws:
IllegalArgumentException- if parsing fails
-
fromDays
Create a Duration from the number of days. -
fromHours
Create a Duration from the number of hours. -
fromMinutes
Create a Duration from the number of minutes. -
fromSeconds
Create a Duration from the number of seconds. -
fromMillis
Create a Duration from the number of milliseconds. -
fromMicros
Create a Duration from the number of microseconds. -
fromNanos
Create a Duration from the number of nanoseconds. -
toDays
Convert a Duration to the number of days. The result will be rounded towards 0 to the nearest day. -
toHours
Convert a Duration to the number of hours. The result will be rounded towards 0 to the nearest hour. -
toMinutes
Convert a Duration to the number of minutes. The result will be rounded towards 0 to the nearest minute. -
toSeconds
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. -
toSecondsAsDouble
Returns the number of seconds of the given duration as adouble. This method should be used to accommodate APIs that only accept durations asdoublevalues.This conversion may lose precision.
If you need the number of seconds in this duration as a
long(not adouble), simply useduration.getSeconds()ortoSeconds(com.google.protobuf.Duration)(which includes validation). -
toMillis
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. -
toMicros
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. -
toNanos
Convert a Duration to the number of nanoseconds. -
add
Add two durations.Do not use this method for new code. Instead, convert to
Durationusingcom.google.protobuf.util.JavaTimeConversions#toJavaDuration, do the arithmetic there, and convert back usingcom.google.protobuf.util.JavaTimeConversions#toProtoDuration.This method will be deprecated once most uses have been eliminated.
-
subtract
Subtract a duration from another.Do not use this method for new code. Instead, convert to
Durationusingcom.google.protobuf.util.JavaTimeConversions#toJavaDuration, do the arithmetic there, and convert back usingcom.google.protobuf.util.JavaTimeConversions#toProtoDuration.This method will be deprecated once most uses have been eliminated.
-
normalizedDuration
-