This class models a quantity or amount of time in terms of a Period and Duration.
A period is a date-based amount of time, consisting of years, months and days.
A duration is a time-based amount of time, consisting of seconds and nanoseconds.
See the Period and Duration classes for more details.
The days in a period take account of daylight saving changes (23 or 25 hour days). When performing calculations, the period is added first, then the duration.
The model is of a directed amount, meaning that the amount may be negative.
This class must be treated as a value type. Do not synchronize, rely on the identity hash code or use the distinction between equals() and ==.
| Modifier and Type | Field and Description |
|---|---|
| private final Duration | duration
The duration. |
| private final Period | period
The period. |
| private static final long | SECONDS_PER_DAY
The number of seconds per day. |
| private static final long | serialVersionUID
A serialization identifier for this class. |
| private static final List | SUPPORTED_UNITS
The supported units. |
| public static final PeriodDuration | ZERO
A constant for a duration of zero. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Temporal | Returns: an object of the same type with the adjustment made, not nullthe temporal object to adjust, not null temporal)Implements java. Adds this amount to the specified temporal object. |
| public static PeriodDuration | |
| public boolean | Returns: true if the other amount is equal to this onethe other amount, null returns false otherAmount)Overrides java. Checks if this amount is equal to the specified |
| public static PeriodDuration | Returns: the equivalent duration, not nullthe temporal amount to convert, not null amount)Obtains an instance from a temporal amount. |
| public long | Returns: the long value of the unitthe unit)TemporalUnit for which to return the valueImplements java. Gets the value of the requested unit. |
| public Duration | |
| public Period | |
| public List | Returns: a list containing the days unit, not nullImplements java. Gets the set of units supported by this amount. |
| public int | Returns: a suitable hash codeOverrides java. A hash code for this amount. |
| public boolean | |
| public PeriodDuration | Returns: aDays based on this instance with the requested amount subtracted, not nullthe amount to add, not null amountToAdd)Returns a copy of this amount with the specified amount subtracted. |
| public PeriodDuration | Returns: the amount multiplied by the specified scalar, not nullthe scalar to multiply by, not null scalar)Returns an instance with the amount multiplied by the specified scalar. |
| public PeriodDuration | |
| public PeriodDuration | Returns: aPeriodDuration based on this one with excess duration normalized to days, not nullReturns a copy of this instance with the days and duration normalized using the standard day of 24 hours. |
| public PeriodDuration | Returns: aPeriodDuration based on this one with excess months normalized to years, not nullReturns a copy of this instance with the years and months exactly normalized. |
| public static PeriodDuration | |
| public static PeriodDuration | |
| public static PeriodDuration | |
| public static PeriodDuration | Returns: the parsed period, not nullthe text to parse, not null text)Obtains an instance from a text string such as |
| public PeriodDuration | Returns: aDays based on this instance with the requested amount added, not nullthe amount to add, not null amountToAdd)Returns a copy of this amount with the specified amount added. |
| private Object | |
| public Temporal | Returns: an object of the same type with the adjustment made, not nullthe temporal object to adjust, not null temporal)Implements java. Subtracts this amount from the specified temporal object. |
| public String | Returns: the period in ISO-8601 string formatOverrides java. Returns a string representation of the amount. |
| public PeriodDuration | Returns: the updated period-durationthe new duration duration)Returns a copy of this period-duration with a different duration. |
| public PeriodDuration | Returns: the updated period-durationthe new period period)Returns a copy of this period-duration with a different period. |
| duration | back to summary |
|---|---|
| private final Duration duration The duration. | |
| period | back to summary |
|---|---|
| private final Period period The period. | |
| SECONDS_PER_DAY | back to summary |
|---|---|
| private static final long SECONDS_PER_DAY The number of seconds per day. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID A serialization identifier for this class. | |
| SUPPORTED_UNITS | back to summary |
|---|---|
| private static final List<TemporalUnit> SUPPORTED_UNITS The supported units. | |
| ZERO | back to summary |
|---|---|
| public static final PeriodDuration ZERO A constant for a duration of zero. | |
| PeriodDuration | back to summary |
|---|---|
| private PeriodDuration(Period period, Duration duration) Constructs an instance. | |
| addTo | back to summary |
|---|---|
| public Temporal addTo(Temporal temporal) Implements java. Adds this amount to the specified temporal object. This returns a temporal object of the same observable type as the input with this amount added. This simply adds the period and duration to the temporal. This instance is immutable and unaffected by this method call.
| |
| between | back to summary |
|---|---|
| public static PeriodDuration between(Temporal startInclusive, Temporal endExclusive) Obtains an instance consisting of the amount of time between two temporals. The start is included, but the end is not. The result of this method can be negative if the end is before the start.
The calculation examines the temporals and extracts
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object otherAmount) Overrides java. Checks if this amount is equal to the specified The comparison is based on the underlying period and duration. | |
| from | back to summary |
|---|---|
| public static PeriodDuration from(TemporalAmount amount) Obtains an instance from a temporal amount.
This obtains an instance based on the specified amount.
A
The result is calculated by looping around each unit in the specified amount.
Any amount that is zero is ignore.
If a unit has an exact duration, it will be totalled using
| |
| get | back to summary |
|---|---|
| public long get(TemporalUnit unit) Implements java. Gets the value of the requested unit.
This returns a value for the supported units -
| |
| getDuration | back to summary |
|---|---|
| public Duration getDuration() Gets the duration part.
| |
| getPeriod | back to summary |
|---|---|
| public Period getPeriod() Gets the period part.
| |
| getUnits | back to summary |
|---|---|
| public List Implements java. Gets the set of units supported by this amount.
This returns the list
This set can be used in conjunction with
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. A hash code for this amount.
| |
| isZero | back to summary |
|---|---|
| public boolean isZero() Checks if all parts of this amount are zero.
This returns true if both
| |
| minus | back to summary |
|---|---|
| public PeriodDuration minus(TemporalAmount amountToAdd) Returns a copy of this amount with the specified amount subtracted.
The parameter is converted using This instance is immutable and unaffected by this method call.
| |
| multipliedBy | back to summary |
|---|---|
| public PeriodDuration multipliedBy(int scalar) Returns an instance with the amount multiplied by the specified scalar. This instance is immutable and unaffected by this method call.
| |
| negated | back to summary |
|---|---|
| public PeriodDuration negated() Returns an instance with the amount negated. This instance is immutable and unaffected by this method call.
| |
| normalizedStandardDays | back to summary |
|---|---|
| public PeriodDuration normalizedStandardDays() Returns a copy of this instance with the days and duration normalized using the standard day of 24 hours. This normalizes the days and duration, leaving the years and months unchanged. The result uses a standard day length of 24 hours. This combines the duration seconds with the number of days and shares the total seconds between the two fields. For example, a period of "2 days and 86401 seconds" will be normalized to "3 days and 1 second". The sign of the days and duration will be the same after normalization. For example, a period of "1 day and -172801 seconds" will be normalized to "-1 day and -1 second". Note that no normalization is performed on the years or months. This instance is immutable and unaffected by this method call.
| |
| normalizedYears | back to summary |
|---|---|
| public PeriodDuration normalizedYears() Returns a copy of this instance with the years and months exactly normalized. This normalizes the years and months units, leaving the days unit unchanged. The result is exact, always representing the same amount of time. The months unit is adjusted to have an absolute value less than 11, with the years unit being adjusted to compensate. For example, a period of "1 year and 15 months" will be normalized to "2 years and 3 months". The sign of the years and months units will be the same after normalization. For example, a period of "1 year and -25 months" will be normalized to "-1 year and -1 month". Note that no normalization is performed on the days or duration. This instance is immutable and unaffected by this method call.
| |
| of | back to summary |
|---|---|
| public static PeriodDuration of(Period period, Duration duration) Obtains an instance based on a period and duration. The total amount of time of the resulting instance is the period plus the duration.
| |
| of | back to summary |
|---|---|
| public static PeriodDuration of(Period period) Obtains an instance based on a period. The duration will be zero.
| |
| of | back to summary |
|---|---|
| public static PeriodDuration of(Duration duration) Obtains an instance based on a duration. The period will be zero.
| |
| parse | back to summary |
|---|---|
| public static PeriodDuration parse(CharSequence text) Obtains an instance from a text string such as
This will parse the string produced by
The string starts with an optional sign, denoted by the ASCII negative
or positive symbol. If negative, the whole amount is negated.
The ASCII letter "P" is next in upper or lower case.
There are then a number of sections, each consisting of a number and a suffix.
At least one of the sections must be present.
The sections have suffixes in ASCII of "Y" for years, "M" for months,
"W" for weeks, "D" for days, "H" for hours, "M" for minutes, "S" for seconds,
accepted in upper or lower case. Note that the ASCII letter "T" separates
the date and time parts and must be present if any time part is present.
The suffixes must occur in order.
The number part of each section must consist of ASCII digits.
The number may be prefixed by the ASCII negative or positive symbol.
The number must parse to an The leading plus/minus sign, and negative values for weeks and days are not part of the ISO-8601 standard.
Note that the date style format For example, the following are valid inputs: "P2Y" -- PeriodDuration.of(Period.ofYears(2)) "P3M" -- PeriodDuration.of(Period.ofMonths(3)) "P4W" -- PeriodDuration.of(Period.ofWeeks(4)) "P5D" -- PeriodDuration.of(Period.ofDays(5)) "PT6H" -- PeriodDuration.of(Duration.ofHours(6)) "P1Y2M3D" -- PeriodDuration.of(Period.of(1, 2, 3)) "P1Y2M3W4DT8H" -- PeriodDuration.of(Period.of(1, 2, 25), Duration.ofHours(8)) "P-1Y2M" -- PeriodDuration.of(Period.of(-1, 2, 0)) "-P1Y2M" -- PeriodDuration.of(Period.of(-1, -2, 0))
| |
| plus | back to summary |
|---|---|
| public PeriodDuration plus(TemporalAmount amountToAdd) Returns a copy of this amount with the specified amount added.
The parameter is converted using This instance is immutable and unaffected by this method call.
| |
| readResolve | back to summary |
|---|---|
| private Object readResolve() Resolves singletons.
| |
| subtractFrom | back to summary |
|---|---|
| public Temporal subtractFrom(Temporal temporal) Implements java. Subtracts this amount from the specified temporal object. This returns a temporal object of the same observable type as the input with this amount subtracted. This simply subtracts the period and duration from the temporal. This instance is immutable and unaffected by this method call.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a string representation of the amount. This will be in the format 'PnYnMnDTnHnMnS', with sections omitted as necessary. An empty amount will return "PT0S". | |
| withDuration | back to summary |
|---|---|
| public PeriodDuration withDuration(Duration duration) Returns a copy of this period-duration with a different duration. This instance is immutable and unaffected by this method call.
| |
| withPeriod | back to summary |
|---|---|
| public PeriodDuration withPeriod(Period period) Returns a copy of this period-duration with a different period. This instance is immutable and unaffected by this method call.
| |