Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/javax/xml/datatype/Duration#pubctors) \| [Methods](https://developer.android.com/reference/javax/xml/datatype/Duration#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/javax/xml/datatype/Duration#inhmethods)

# Duration

` public abstract class Duration `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | javax.xml.datatype.Duration |

<br />

*** ** * ** ***

<br />

Immutable representation of a time span as defined in the W3C XML Schema 1.0 specification.

A Duration object represents a period of Gregorian time, which consists of six fields (years, months, days, hours, minutes, and seconds) plus a sign (+/-) field.

The first five fields have non-negative (\>=0) integers or null (which represents that the field is not set), and the seconds field has a non-negative decimal or null. A negative sign indicates a negative duration.

This class provides a number of methods that make it easy to use for the duration datatype of XML Schema 1.0 with the errata.

## Order relationship

Duration objects only have partial order, where two values A and B maybe either:

1. A\<B (A is shorter than B)
2. A\>B (A is longer than B)
3. A==B (A and B are of the same duration)
4. A\<\>B (Comparison between A and B is indeterminate)

For example, 30 days cannot be meaningfully compared to one month. The `https://developer.android.com/reference/javax/xml/datatype/Duration#compare(javax.xml.datatype.Duration)` method implements this relationship.

See the `https://developer.android.com/reference/javax/xml/datatype/Duration#isLongerThan(javax.xml.datatype.Duration)` method for details about the order relationship among `Duration` objects.

## Operations over Duration

This class provides a set of basic arithmetic operations, such as addition, subtraction and multiplication. Because durations don't have total order, an operation could fail for some combinations of operations. For example, you cannot subtract 15 days from 1 month. See the javadoc of those methods for detailed conditions where this could happen.

Also, division of a duration by a number is not provided because the `Duration` class can only deal with finite precision decimal numbers. For example, one cannot represent 1 sec divided by 3.

However, you could substitute a division by 3 with multiplying by numbers such as 0.3 or 0.333.

## Range of allowed values

Because some operations of `Duration` rely on `https://developer.android.com/reference/java/util/Calendar` even though `https://developer.android.com/reference/javax/xml/datatype/Duration` can hold very large or very small values, some of the methods may not work correctly on such `Duration`s. The impacted methods document their dependency on `https://developer.android.com/reference/java/util/Calendar`.
**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/XMLGregorianCalendar#add(javax.xml.datatype.Duration)`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/javax/xml/datatype/Duration#Duration()() ` |

| ### Public methods ||
|---|---|
| ` abstract https://developer.android.com/reference/javax/xml/datatype/Duration` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#add(javax.xml.datatype.Duration)(https://developer.android.com/reference/javax/xml/datatype/Duration rhs) ` <br /> Computes a new duration whose value is `this+rhs`. |
| ` abstract void` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Calendar)(https://developer.android.com/reference/java/util/Calendar calendar) ` Adds this duration to a `https://developer.android.com/reference/java/util/Calendar` object. |
| ` void` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Date)(https://developer.android.com/reference/java/util/Date date) ` Adds this duration to a `https://developer.android.com/reference/java/util/Date` object. |
| ` abstract int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#compare(javax.xml.datatype.Duration)(https://developer.android.com/reference/javax/xml/datatype/Duration duration) ` <br /> Partial order relation comparison with this `Duration` instance. |
| ` boolean` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object duration) ` <br /> Checks if this duration object has the same duration as another `Duration` object. |
| ` int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getDays()() ` Obtains the value of the DAYS field as an integer value, or 0 if not present. |
| ` abstract https://developer.android.com/reference/java/lang/Number` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getField(javax.xml.datatype.DatatypeConstants.Field)(https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants.Field field) ` Gets the value of a field. |
| ` int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getHours()() ` Obtains the value of the HOURS field as an integer value, or 0 if not present. |
| ` int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getMinutes()() ` Obtains the value of the MINUTES field as an integer value, or 0 if not present. |
| ` int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getMonths()() ` Obtains the value of the MONTHS field as an integer value, or 0 if not present. |
| ` int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getSeconds()() ` Obtains the value of the SECONDS field as an integer value, or 0 if not present. |
| ` abstract int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getSign()() ` Returns the sign of this duration in -1,0, or 1. |
| ` long` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getTimeInMillis(java.util.Calendar)(https://developer.android.com/reference/java/util/Calendar startInstant) ` <br /> Returns the length of the duration in milliseconds. |
| ` long` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getTimeInMillis(java.util.Date)(https://developer.android.com/reference/java/util/Date startInstant) ` <br /> Returns the length of the duration in milliseconds. |
| ` https://developer.android.com/reference/javax/xml/namespace/QName` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getXMLSchemaType()() ` <br /> Return the name of the XML Schema date/time type that this instance maps to. |
| ` int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#getYears()() ` <br /> Get the years value of this `Duration` as an `int` or `0` if not present. |
| ` abstract int` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#hashCode()() ` Returns a hash code consistent with the definition of the equals method. |
| ` boolean` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#isLongerThan(javax.xml.datatype.Duration)(https://developer.android.com/reference/javax/xml/datatype/Duration duration) ` <br /> Checks if this duration object is strictly longer than another `Duration` object. |
| ` abstract boolean` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#isSet(javax.xml.datatype.DatatypeConstants.Field)(https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants.Field field) ` Checks if a field is set. |
| ` boolean` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#isShorterThan(javax.xml.datatype.Duration)(https://developer.android.com/reference/javax/xml/datatype/Duration duration) ` <br /> Checks if this duration object is strictly shorter than another `Duration` object. |
| ` https://developer.android.com/reference/javax/xml/datatype/Duration` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#multiply(int)(int factor) ` <br /> Computes a new duration whose value is `factor` times longer than the value of this duration. |
| ` abstract https://developer.android.com/reference/javax/xml/datatype/Duration` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#multiply(java.math.BigDecimal)(https://developer.android.com/reference/java/math/BigDecimal factor) ` Computes a new duration whose value is `factor` times longer than the value of this duration. |
| ` abstract https://developer.android.com/reference/javax/xml/datatype/Duration` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#negate()() ` Returns a new `Duration` object whose value is `-this`. |
| ` abstract https://developer.android.com/reference/javax/xml/datatype/Duration` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#normalizeWith(java.util.Calendar)(https://developer.android.com/reference/java/util/Calendar startTimeInstant) ` <br /> Converts the years and months fields into the days field by using a specific time instant as the reference point. |
| ` https://developer.android.com/reference/javax/xml/datatype/Duration` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#subtract(javax.xml.datatype.Duration)(https://developer.android.com/reference/javax/xml/datatype/Duration rhs) ` <br /> Computes a new duration whose value is `this-rhs`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/javax/xml/datatype/Duration#toString()() ` <br /> Returns a `String` representation of this `Duration` `Object`. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### Duration

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Duration ()
```

<br />

<br />

## Public methods

### add

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract Duration add (Duration rhs)
```

<br />

Computes a new duration whose value is `this+rhs`.

For example,

```
"1 day" + "-3 days" = "-2 days"
"1 year" + "1 day" = "1 year and 1 day"
"-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
"15 hours" + "-3 days" = "-(2 days,9 hours)"
"1 year" + "-1 day" = IllegalStateException
```

Since there's no way to meaningfully subtract 1 day from 1 month, there are cases where the operation fails in `https://developer.android.com/reference/java/lang/IllegalStateException`.

Formally, the computation is defined as follows.

Firstly, we can assume that two `Duration`s to be added are both positive without losing generality (i.e., `(-X)+Y=Y-X`, `X+(-Y)=X-Y`, `(-X)+(-Y)=-(X+Y)`)

Addition of two positive `Duration`s are simply defined as field by field addition where missing fields are treated as 0.

A field of the resulting `Duration` will be unset if and only if respective fields of two input `Duration`s are unset.

Note that `lhs.add(rhs)` will be always successful if `lhs.signum()*rhs.signum()!=-1` or both of them are normalized.

<br />

<br />

| Parameters ||
|---|---|
| `rhs` | `Duration`: `Duration` to add to this `Duration` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/datatype/Duration` | non-null valid Duration object. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If two durations cannot be meaningfully added. For example, adding negative one day to one month causes this exception. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | If the rhs parameter is null. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#subtract(javax.xml.datatype.Duration)`

### addTo

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void addTo (Calendar calendar)
```

Adds this duration to a `https://developer.android.com/reference/java/util/Calendar` object.

Calls `https://developer.android.com/reference/java/util/Calendar#add(int,%20int)` in the order of YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, and MILLISECONDS if those fields are present. Because the `https://developer.android.com/reference/java/util/Calendar` class uses int to hold values, there are cases where this method won't work correctly (for example if values of fields exceed the range of int.)

Also, since this duration class is a Gregorian duration, this method will not work correctly if the given `https://developer.android.com/reference/java/util/Calendar` object is based on some other calendar systems.

Any fractional parts of this `Duration` object beyond milliseconds will be simply ignored. For example, if this duration is "P1.23456S", then 1 is added to SECONDS, 234 is added to MILLISECONDS, and the rest will be unused.

Note that because `https://developer.android.com/reference/java/util/Calendar#add(int,%20int)` is using int, `Duration` with values beyond the range of int in its fields will cause overflow/underflow to the given `https://developer.android.com/reference/java/util/Calendar`. `https://developer.android.com/reference/javax/xml/datatype/XMLGregorianCalendar#add(javax.xml.datatype.Duration)` provides the same basic operation as this method while avoiding the overflow/underflow issues.

<br />

| Parameters ||
|---|---|
| `calendar` | `Calendar`: A calendar object whose value will be modified. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the calendar parameter is null. |

### addTo

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addTo (Date date)
```

Adds this duration to a `https://developer.android.com/reference/java/util/Date` object.

The given date is first converted into a `https://developer.android.com/reference/java/util/GregorianCalendar`, then the duration is added exactly like the `https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Calendar)` method.

The updated time instant is then converted back into a `https://developer.android.com/reference/java/util/Date` object and used to update the given `https://developer.android.com/reference/java/util/Date` object.

This somewhat redundant computation is necessary to unambiguously determine the duration of months and years.

<br />

| Parameters ||
|---|---|
| `date` | `Date`: A date object whose value will be modified. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the date parameter is null. |

### compare

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int compare (Duration duration)
```

<br />

Partial order relation comparison with this `Duration` instance.

Comparison result must be in accordance with [W3C XML Schema 1.0 Part 2, Section 3.2.7.6.2, *Order relation on duration*](http://www.w3.org/TR/xmlschema-2/#duration-order).

Return:

- `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#LESSER` if this `Duration` is shorter than `duration` parameter
- `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#EQUAL` if this `Duration` is equal to `duration` parameter
- `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#GREATER` if this `Duration` is longer than `duration` parameter
- `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#INDETERMINATE` if a conclusive partial order relation cannot be determined

<br />

<br />

| Parameters ||
|---|---|
| `duration` | `Duration`: to compare <br /> |

| Returns ||
|---|---|
| `int` | the relationship between `this` `Duration`and `duration` parameter as `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#LESSER`, `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#EQUAL`, `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#GREATER` or `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#INDETERMINATE`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `duration` is `null`. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#isShorterThan(javax.xml.datatype.Duration)`
- `https://developer.android.com/reference/javax/xml/datatype/Duration#isLongerThan(javax.xml.datatype.Duration)`

### equals

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean equals (Object duration)
```

<br />

Checks if this duration object has the same duration as another `Duration` object.

For example, "P1D" (1 day) is equal to "PT24H" (24 hours).

Duration X is equal to Y if and only if time instant t+X and t+Y are the same for all the test time instants specified in the section 3.2.6.2 of the XML Schema 1.0 specification.

Note that there are cases where two `Duration`s are "incomparable" to each other, like one month and 30 days. For example,

```
!new Duration("P1M").isShorterThan(new Duration("P30D"))
!new Duration("P1M").isLongerThan(new Duration("P30D"))
!new Duration("P1M").equals(new Duration("P30D"))
```

<br />

<br />

| Parameters ||
|---|---|
| `duration` | `Object`: A non-null valid `Duration` object. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if this duration is the same length as `duration`. `false` if `duration` is not a `Duration` object, is `null`, or its length is different from this duration. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#compare(javax.xml.datatype.Duration)`

### getDays

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getDays ()
```

Obtains the value of the DAYS field as an integer value, or 0 if not present. This method works just like `https://developer.android.com/reference/javax/xml/datatype/Duration#getYears()` except that this method works on the DAYS field.

<br />

| Returns ||
|---|---|
| `int` | Days of this `Duration`. <br /> |

### getField

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract Number getField (DatatypeConstants.Field field)
```

Gets the value of a field. Fields of a duration object may contain arbitrary large value. Therefore this method is designed to return a `https://developer.android.com/reference/java/lang/Number` object. In case of YEARS, MONTHS, DAYS, HOURS, and MINUTES, the returned number will be a non-negative integer. In case of seconds, the returned number may be a non-negative decimal value.

<br />

| Parameters ||
|---|---|
| `field` | `DatatypeConstants.Field`: one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, MINUTES, or SECONDS.) <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Number` | If the specified field is present, this method returns a non-null non-negative `https://developer.android.com/reference/java/lang/Number` object that represents its value. If it is not present, return null. For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method returns a `https://developer.android.com/reference/java/math/BigInteger` object. For SECONDS, this method returns a `https://developer.android.com/reference/java/math/BigDecimal`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | If the `field` is `null`. |

### getHours

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getHours ()
```

Obtains the value of the HOURS field as an integer value, or 0 if not present. This method works just like `https://developer.android.com/reference/javax/xml/datatype/Duration#getYears()` except that this method works on the HOURS field.

<br />

| Returns ||
|---|---|
| `int` | Hours of this `Duration`. <br /> |

### getMinutes

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getMinutes ()
```

Obtains the value of the MINUTES field as an integer value, or 0 if not present. This method works just like `https://developer.android.com/reference/javax/xml/datatype/Duration#getYears()` except that this method works on the MINUTES field.

<br />

| Returns ||
|---|---|
| `int` | Minutes of this `Duration`. <br /> |

### getMonths

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getMonths ()
```

Obtains the value of the MONTHS field as an integer value, or 0 if not present. This method works just like `https://developer.android.com/reference/javax/xml/datatype/Duration#getYears()` except that this method works on the MONTHS field.

<br />

| Returns ||
|---|---|
| `int` | Months of this `Duration`. <br /> |

### getSeconds

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getSeconds ()
```

Obtains the value of the SECONDS field as an integer value, or 0 if not present. This method works just like `https://developer.android.com/reference/javax/xml/datatype/Duration#getYears()` except that this method works on the SECONDS field.

<br />

| Returns ||
|---|---|
| `int` | seconds in the integer value. The fraction of seconds will be discarded (for example, if the actual value is 2.5, this method returns 2) <br /> |

### getSign

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int getSign ()
```

Returns the sign of this duration in -1,0, or 1.

<br />

| Returns ||
|---|---|
| `int` | -1 if this duration is negative, 0 if the duration is zero, and 1 if the duration is positive. <br /> |

### getTimeInMillis

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public long getTimeInMillis (Calendar startInstant)
```

<br />

Returns the length of the duration in milliseconds.

If the seconds field carries more digits than millisecond order, those will be simply discarded (or in other words, rounded to zero.) For example, for any Calendar value `x`,

    new Duration("PT10.00099S").getTimeInMills(x) == 10000.
    new Duration("-PT10.00099S").getTimeInMills(x) == -10000.

Note that this method uses the `https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Calendar)` method, which may work incorrectly with `Duration` objects with very large values in its fields. See the `https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Calendar)` method for details.

<br />

| Parameters ||
|---|---|
| `startInstant` | `Calendar`: The length of a month/year varies. The `startInstant` is used to disambiguate this variance. Specifically, this method returns the difference between `startInstant` and `startInstant+duration` <br /> |

| Returns ||
|---|---|
| `long` | milliseconds between `startInstant` and `startInstant` plus this `Duration` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `startInstant` parameter is null. |

### getTimeInMillis

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public long getTimeInMillis (Date startInstant)
```

<br />

Returns the length of the duration in milliseconds.

If the seconds field carries more digits than millisecond order, those will be simply discarded (or in other words, rounded to zero.) For example, for any `Date` value `x`,

    new Duration("PT10.00099S").getTimeInMills(x) == 10000.
    new Duration("-PT10.00099S").getTimeInMills(x) == -10000.

Note that this method uses the `https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Date)` method, which may work incorrectly with `Duration` objects with very large values in its fields. See the `https://developer.android.com/reference/javax/xml/datatype/Duration#addTo(java.util.Date)` method for details.

<br />

| Parameters ||
|---|---|
| `startInstant` | `Date`: The length of a month/year varies. The `startInstant` is used to disambiguate this variance. Specifically, this method returns the difference between `startInstant` and `startInstant+duration`. <br /> |

| Returns ||
|---|---|
| `long` | milliseconds between `startInstant` and `startInstant` plus this `Duration` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | If the startInstant parameter is null. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#getTimeInMillis(java.util.Calendar)`

### getXMLSchemaType

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public QName getXMLSchemaType ()
```

<br />

Return the name of the XML Schema date/time type that this instance maps to. Type is computed based on fields that are set, i.e. `https://developer.android.com/reference/javax/xml/datatype/Duration#isSet(javax.xml.datatype.DatatypeConstants.Field)` == `true`.

| Required fields for XML Schema 1.0 Date/Time Datatypes. *(timezone is optional for all date/time datatypes)* |||||||
|:---:|---|---|---|---|---|---|
| Datatype | year | month | day | hour | minute | second |
| `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#DURATION` | X | X | X | X | X | X |
| `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#DURATION_DAYTIME` |   |   | X | X | X | X |
| `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#DURATION_YEARMONTH` | X | X |   |   |   |   |

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/namespace/QName` | one of the following constants: `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#DURATION`, `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#DURATION_DAYTIME` or `https://developer.android.com/reference/javax/xml/datatype/DatatypeConstants#DURATION_YEARMONTH`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If the combination of set fields does not match one of the XML Schema date/time datatypes. |

### getYears

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getYears ()
```

<br />

Get the years value of this `Duration` as an `int` or `0` if not present.

`getYears()` is a convenience method for `https://developer.android.com/reference/javax/xml/datatype/Duration#getField(javax.xml.datatype.DatatypeConstants.Field)`.

As the return value is an `int`, an incorrect value will be returned for `Duration`s with years that go beyond the range of an `int`. Use `https://developer.android.com/reference/javax/xml/datatype/Duration#getField(javax.xml.datatype.DatatypeConstants.Field)` to avoid possible loss of precision.

<br />

<br />

| Returns ||
|---|---|
| `int` | If the years field is present, return its value as an `int`, else return `0`. <br /> |

### hashCode

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int hashCode ()
```

Returns a hash code consistent with the definition of the equals method.

<br />

| Returns ||
|---|---|
| `int` | a hash code value for this object. <br /> |

**See also:**

- `https://developer.android.com/reference/java/lang/Object#hashCode()`

### isLongerThan

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isLongerThan (Duration duration)
```

<br />

Checks if this duration object is strictly longer than another `Duration` object.

Duration X is "longer" than Y if and only if X\>Y as defined in the section 3.2.6.2 of the XML Schema 1.0 specification.

For example, "P1D" (one day) \> "PT12H" (12 hours) and "P2Y" (two years) \> "P23M" (23 months).

<br />

<br />

| Parameters ||
|---|---|
| `duration` | `Duration`: `Duration` to test this `Duration` against. <br /> |

| Returns ||
|---|---|
| `boolean` | true if the duration represented by this object is longer than the given duration. false otherwise. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | If `duration` is null. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#isShorterThan(javax.xml.datatype.Duration)`
- `https://developer.android.com/reference/javax/xml/datatype/Duration#compare(javax.xml.datatype.Duration)`

### isSet

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean isSet (DatatypeConstants.Field field)
```

Checks if a field is set. A field of a duration object may or may not be present. This method can be used to test if a field is present.

<br />

| Parameters ||
|---|---|
| `field` | `DatatypeConstants.Field`: one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, MINUTES, or SECONDS.) <br /> |

| Returns ||
|---|---|
| `boolean` | true if the field is present. false if not. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | If the field parameter is null. |

### isShorterThan

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isShorterThan (Duration duration)
```

<br />

Checks if this duration object is strictly shorter than another `Duration` object.

<br />

<br />

| Parameters ||
|---|---|
| `duration` | `Duration`: `Duration` to test this `Duration` against. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if `duration` parameter is shorter than this `Duration`, else `false`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `duration` is null. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#isLongerThan(javax.xml.datatype.Duration)`
- `https://developer.android.com/reference/javax/xml/datatype/Duration#compare(javax.xml.datatype.Duration)`

### multiply

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Duration multiply (int factor)
```

<br />

Computes a new duration whose value is `factor` times longer than the value of this duration.

This method is provided for the convenience. It is functionally equivalent to the following code:

```
multiply(new BigDecimal(String.valueOf(factor)))
```

<br />

<br />

| Parameters ||
|---|---|
| `factor` | `int`: Factor times longer of new `Duration` to create. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/datatype/Duration` | New `Duration` that is `factor`times longer than this `Duration`. <br /> |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#multiply(java.math.BigDecimal)`

### multiply

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract Duration multiply (BigDecimal factor)
```

Computes a new duration whose value is `factor` times longer than the value of this duration.

For example,

```
"P1M" (1 month) * "12" = "P12M" (12 months)
"PT1M" (1 min) * "0.3" = "PT18S" (18 seconds)
"P1M" (1 month) * "1.5" = IllegalStateException
```

Since the `Duration` class is immutable, this method doesn't change the value of this object. It simply computes a new Duration object and returns it.

The operation will be performed field by field with the precision of `https://developer.android.com/reference/java/math/BigDecimal`. Since all the fields except seconds are restricted to hold integers, any fraction produced by the computation will be carried down toward the next lower unit. For example, if you multiply "P1D" (1 day) with "0.5", then it will be 0.5 day, which will be carried down to "PT12H" (12 hours). When fractions of month cannot be meaningfully carried down to days, or year to months, this will cause an `https://developer.android.com/reference/java/lang/IllegalStateException` to be thrown. For example if you multiple one month by 0.5.

To avoid `https://developer.android.com/reference/java/lang/IllegalStateException`, use the `https://developer.android.com/reference/javax/xml/datatype/Duration#normalizeWith(java.util.Calendar)` method to remove the years and months fields.

<br />

| Parameters ||
|---|---|
| `factor` | `BigDecimal`: to multiply by <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/datatype/Duration` | returns a non-null valid `Duration` object <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if operation produces fraction in the months field. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the `factor` parameter is `null`. |

### negate

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract Duration negate ()
```

Returns a new `Duration` object whose value is `-this`.

Since the `Duration` class is immutable, this method doesn't change the value of this object. It simply computes a new Duration object and returns it.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/datatype/Duration` | always return a non-null valid `Duration` object. <br /> |

### normalizeWith

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract Duration normalizeWith (Calendar startTimeInstant)
```

<br />

Converts the years and months fields into the days field by using a specific time instant as the reference point.

For example, duration of one month normalizes to 31 days given the start time instance "July 8th 2003, 17:40:32".

Formally, the computation is done as follows:

1. the given Calendar object is cloned
2. the years, months and days fields will be added to the `https://developer.android.com/reference/java/util/Calendar` object by using the `https://developer.android.com/reference/java/util/Calendar#add(int,%20int)` method
3. the difference between the two Calendars in computed in milliseconds and converted to days, if a remainder occurs due to Daylight Savings Time, it is discarded
4. the computed days, along with the hours, minutes and seconds fields of this duration object is used to construct a new Duration object.

Note that since the Calendar class uses `int` to hold the value of year and month, this method may produce an unexpected result if this duration object holds a very large value in the years or months fields.

<br />

<br />

| Parameters ||
|---|---|
| `startTimeInstant` | `Calendar`: `Calendar` reference point. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/datatype/Duration` | `Duration` of years and months of this `Duration` as days. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | If the startTimeInstant parameter is null. |

### subtract

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Duration subtract (Duration rhs)
```

<br />

Computes a new duration whose value is `this-rhs`.

For example:

```
"1 day" - "-3 days" = "4 days"
"1 year" - "1 day" = IllegalStateException
"-(1 hour,50 minutes)" - "-20 minutes" = "-(1hours,30 minutes)"
"15 hours" - "-3 days" = "3 days and 15 hours"
"1 year" - "-1 day" = "1 year and 1 day"
```

Since there's no way to meaningfully subtract 1 day from 1 month, there are cases where the operation fails in `https://developer.android.com/reference/java/lang/IllegalStateException`.

Formally the computation is defined as follows. First, we can assume that two `Duration`s are both positive without losing generality. (i.e., `(-X)-Y=-(X+Y)`, `X-(-Y)=X+Y`, `(-X)-(-Y)=-(X-Y)`)

Then two durations are subtracted field by field. If the sign of any non-zero field F is different from the sign of the most significant field, 1 (if F is negative) or -1 (otherwise) will be borrowed from the next bigger unit of F.

This process is repeated until all the non-zero fields have the same sign.

If a borrow occurs in the days field (in other words, if the computation needs to borrow 1 or -1 month to compensate days), then the computation fails by throwing an `https://developer.android.com/reference/java/lang/IllegalStateException`.

<br />

<br />

| Parameters ||
|---|---|
| `rhs` | `Duration`: `Duration` to subtract from this `Duration`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/xml/datatype/Duration` | New `Duration` created from subtracting `rhs` from this `Duration`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | If two durations cannot be meaningfully subtracted. For example, subtracting one day from one month causes this exception. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | If the rhs parameter is null. |

**See also:**

- `https://developer.android.com/reference/javax/xml/datatype/Duration#add(javax.xml.datatype.Duration)`

### toString

Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

<br />

Returns a `String` representation of this `Duration` `Object`.

The result is formatted according to the XML Schema 1.0 specification and can be always parsed back later into the equivalent `Duration` `Object` by `https://developer.android.com/reference/javax/xml/datatype/DatatypeFactory#newDuration(java.lang.String)`.

Formally, the following holds for any `Duration` `Object` x:

```
new Duration(x.toString()).equals(x)
```

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | A non-`null` valid `String` representation of this `Duration`. <br /> |