Class Durations (3.19.4)

  public 
  
 final 
  
 class 
 Durations 
 

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

Inheritance

java.lang.Object > Durations

Static Fields

MAX_VALUE

  public 
  
 static 
  
 final 
  
 Duration 
  
 MAX_VALUE 
 

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

Field Value
Type
Description

MIN_VALUE

  public 
  
 static 
  
 final 
  
 Duration 
  
 MIN_VALUE 
 

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

Field Value
Type
Description

ZERO

  public 
  
 static 
  
 final 
  
 Duration 
  
 ZERO 
 

A constant holding the duration of zero.

Field Value
Type
Description

Static Methods

add(Duration d1, Duration d2)

  public 
  
 static 
  
 Duration 
  
 add 
 ( 
 Duration 
  
 d1 
 , 
  
 Duration 
  
 d2 
 ) 
 

Add two durations.

Parameters
Name
Description
Returns
Type
Description

checkNotNegative(Duration duration)

  public 
  
 static 
  
 Duration 
  
 checkNotNegative 
 ( 
 Duration 
  
 duration 
 ) 
 

Ensures that the given Duration is not negative.

Parameter
Name
Description
duration
Returns
Type
Description

checkPositive(Duration duration)

  public 
  
 static 
  
 Duration 
  
 checkPositive 
 ( 
 Duration 
  
 duration 
 ) 
 

Ensures that the given Duration is positive.

Parameter
Name
Description
duration
Returns
Type
Description

checkValid(Duration duration)

  public 
  
 static 
  
 Duration 
  
 checkValid 
 ( 
 Duration 
  
 duration 
 ) 
 

Throws an IllegalArgumentException if the given Duration is not valid.

Parameter
Name
Description
duration
Returns
Type
Description

checkValid(Duration.Builder durationBuilder)

  public 
  
 static 
  
 Duration 
  
 checkValid 
 ( 
 Duration 
 . 
 Builder 
  
 durationBuilder 
 ) 
 

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

Parameter
Name
Description
durationBuilder
Returns
Type
Description

A valid, built Duration .

comparator()

  public 
  
 static 
  
 Comparator<Duration> 
  
 comparator 
 () 
 

Returns a Comparator for Duration s which sorts in increasing chronological order. Nulls and invalid Duration s are not allowed (see #isValid ). The returned comparator is serializable.

Returns
Type
Description

compare(Duration x, Duration y)

  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) .

Parameters
Name
Description
Returns
Type
Description
int

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

fromDays(long days)

  public 
  
 static 
  
 Duration 
  
 fromDays 
 ( 
 long 
  
 days 
 ) 
 

Create a Duration from the number of days.

Parameter
Name
Description
days
Returns
Type
Description

fromHours(long hours)

  public 
  
 static 
  
 Duration 
  
 fromHours 
 ( 
 long 
  
 hours 
 ) 
 

Create a Duration from the number of hours.

Parameter
Name
Description
hours
Returns
Type
Description

fromMicros(long microseconds)

  public 
  
 static 
  
 Duration 
  
 fromMicros 
 ( 
 long 
  
 microseconds 
 ) 
 

Create a Duration from the number of microseconds.

Parameter
Name
Description
microseconds
Returns
Type
Description

fromMillis(long milliseconds)

  public 
  
 static 
  
 Duration 
  
 fromMillis 
 ( 
 long 
  
 milliseconds 
 ) 
 

Create a Duration from the number of milliseconds.

Parameter
Name
Description
milliseconds
Returns
Type
Description

fromMinutes(long minutes)

  public 
  
 static 
  
 Duration 
  
 fromMinutes 
 ( 
 long 
  
 minutes 
 ) 
 

Create a Duration from the number of minutes.

Parameter
Name
Description
minutes
Returns
Type
Description

fromNanos(long nanoseconds)

  public 
  
 static 
  
 Duration 
  
 fromNanos 
 ( 
 long 
  
 nanoseconds 
 ) 
 

Create a Duration from the number of nanoseconds.

Parameter
Name
Description
nanoseconds
Returns
Type
Description

fromSeconds(long seconds)

  public 
  
 static 
  
 Duration 
  
 fromSeconds 
 ( 
 long 
  
 seconds 
 ) 
 

Create a Duration from the number of seconds.

Parameter
Name
Description
seconds
Returns
Type
Description

isNegative(Duration duration)

  public 
  
 static 
  
 boolean 
  
 isNegative 
 ( 
 Duration 
  
 duration 
 ) 
 

Returns whether the given Duration is negative or not.

Parameter
Name
Description
duration
Returns
Type
Description

isPositive(Duration duration)

  public 
  
 static 
  
 boolean 
  
 isPositive 
 ( 
 Duration 
  
 duration 
 ) 
 

Returns whether the given Duration is positive or not.

Parameter
Name
Description
duration
Returns
Type
Description

isValid(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

isValid(long seconds, int nanos)

  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.

Parameters
Name
Description
seconds
nanos
int
Returns
Type
Description

parse(String value)

  public 
  
 static 
  
 Duration 
  
 parse 
 ( 
 String 
  
 value 
 ) 
 

Parse from a string to produce a duration.

Parameter
Name
Description
value
Returns
Type
Description

A Duration parsed from the string.

Exceptions
Type
Description

if parsing fails.

parseUnchecked(String value)

  public 
  
 static 
  
 Duration 
  
 parseUnchecked 
 ( 
 String 
  
 value 
 ) 
 

Parses a string in RFC 3339 format into a Duration .

Identical to #parse(String) , but throws an IllegalArgumentException instead of a ParseException if parsing fails.

Parameter
Name
Description
value
Returns
Type
Description

a Duration parsed from the string

subtract(Duration d1, Duration d2)

  public 
  
 static 
  
 Duration 
  
 subtract 
 ( 
 Duration 
  
 d1 
 , 
  
 Duration 
  
 d2 
 ) 
 

Subtract a duration from another.

Parameters
Name
Description
Returns
Type
Description

toDays(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

toHours(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

toMicros(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

toMillis(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

toMinutes(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

toNanos(Duration duration)

  public 
  
 static 
  
 long 
  
 toNanos 
 ( 
 Duration 
  
 duration 
 ) 
 

Convert a Duration to the number of nanoseconds.

Parameter
Name
Description
duration
Returns
Type
Description

toSeconds(Duration duration)

  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.

Parameter
Name
Description
duration
Returns
Type
Description

toSecondsAsDouble(Duration duration)

  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).

Parameter
Name
Description
duration
Returns
Type
Description

toString(Duration duration)

  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).

Parameter
Name
Description
duration
Returns
Type
Description

The string representation of the given duration.

Design a Mobile Site
View Site in Mobile | Classic
Share by: