# 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](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.Durations#com_google_protobuf_util_Durations_isValid_com_google_protobuf_Duration_).

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> Durations

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Fields

### MAX_VALUE

    public static final Duration MAX_VALUE

A constant holding the maximum valid [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration), approximately `+10,000` years.

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### MIN_VALUE

    public static final Duration MIN_VALUE

A constant holding the minimum valid [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration), approximately `-10,000` years.

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### ZERO

    public static final Duration ZERO

A constant holding the duration of zero.

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

## Static Methods

### add(Duration d1, Duration d2)

    public static Duration add(Duration d1, Duration d2)

Add two durations.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `d1` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |
| `d2` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### checkNotNegative(Duration duration)

    public static Duration checkNotNegative(Duration duration)

Ensures that the given [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration) is not negative.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### checkPositive(Duration duration)

    public static Duration checkPositive(Duration duration)

Ensures that the given [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration) is positive.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### checkValid(Duration duration)

    public static Duration checkValid(Duration duration)

Throws an IllegalArgumentException if the given [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration) is not valid.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### 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)](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.Durations#com_google_protobuf_util_Durations_checkValid_com_google_protobuf_Duration_).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `durationBuilder` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration.Builder` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` | A valid, built [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration). |

### comparator()

    public static Comparator<Duration> comparator()

Returns a [Comparator](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html) for [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration)s which sorts in increasing chronological
order. Nulls and invalid [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration)s are not allowed (see [#isValid](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.Durations#com_google_protobuf_util_Durations_isValid_)). The returned
comparator is serializable.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html<https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration>` |   |

### 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** |
| `x` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |
| `y` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` | 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### fromHours(long hours)

    public static Duration fromHours(long hours)

Create a Duration from the number of hours.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `hours` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### fromMicros(long microseconds)

    public static Duration fromMicros(long microseconds)

Create a Duration from the number of microseconds.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `microseconds` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### fromMillis(long milliseconds)

    public static Duration fromMillis(long milliseconds)

Create a Duration from the number of milliseconds.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `milliseconds` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### fromMinutes(long minutes)

    public static Duration fromMinutes(long minutes)

Create a Duration from the number of minutes.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `minutes` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### fromNanos(long nanoseconds)

    public static Duration fromNanos(long nanoseconds)

Create a Duration from the number of nanoseconds.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `nanoseconds` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### fromSeconds(long seconds)

    public static Duration fromSeconds(long seconds)

Create a Duration from the number of seconds.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `seconds` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### isNegative(Duration duration)

    public static boolean isNegative(Duration duration)

Returns whether the given [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration) is negative or not.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### isPositive(Duration duration)

    public static boolean isPositive(Duration duration)

Returns whether the given [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration) is positive or not.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### isValid(Duration duration)

    public static boolean isValid(Duration duration)

Returns true if the given [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |
| `nanos` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### parse(String value)

    public static Duration parse(String value)

Parse from a string to produce a duration.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` | A Duration parsed from the string. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/text/ParseException.html` | if parsing fails. |

### parseUnchecked(String value)

    public static Duration parseUnchecked(String value)

Parses a string in RFC 3339 format into a [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration).

Identical to [#parse(String)](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.Durations#com_google_protobuf_util_Durations_parse_java_lang_String_), but throws an IllegalArgumentException instead
of a [ParseException](https://docs.oracle.com/javase/8/docs/api/java/text/ParseException.html) if parsing fails.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` | a [Duration](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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** |
| `d1` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |
| `d2` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### toNanos(Duration duration)

    public static long toNanos(Duration duration)

Convert a Duration to the number of nanoseconds.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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](https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.Durations#com_google_protobuf_util_Durations_toSeconds_) (which includes validation).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `duration` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### 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` | `https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Duration` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | The string representation of the given duration. |