Top Description Fields Constructors Methods
org.threeten.extra

public final Class PeriodDuration

extends Object
implements TemporalAmount, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.time.temporal.TemporalAmount
Static Imports
java.time.temporal.ChronoUnit.DAYS, .ChronoUnit.MONTHS, .ChronoUnit.NANOS, .ChronoUnit.SECONDS, .ChronoUnit.YEARS

An amount of time in the ISO-8601 calendar system that combines a period and a duration.

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.

Implementation Requirements:

This class is immutable and thread-safe.

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

Field Summary

Modifier and TypeField 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<TemporalUnit>
SUPPORTED_UNITS

The supported units.

public static final PeriodDuration
ZERO

A constant for a duration of zero.

Constructor Summary

AccessConstructor and Description
private
PeriodDuration(Period
the period
period
,
Duration
the duration
duration
)

Constructs an instance.

Method Summary

Modifier and TypeMethod and Description
public Temporal

Returns:

an object of the same type with the adjustment made, not null
addTo
(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.temporal.TemporalAmount.addTo.

Adds this amount to the specified temporal object.

public static PeriodDuration

Returns:

the number of days between this date and the end date, not null
between
(Temporal
the start, inclusive, not null
startInclusive
,
Temporal
the end, exclusive, not null
endExclusive
)

Obtains an instance consisting of the amount of time between two temporals.

public boolean

Returns:

true if the other amount is equal to this one
equals
(Object
the other amount, null returns false
otherAmount
)

Overrides java.lang.Object.equals.

Checks if this amount is equal to the specified PeriodDuration.

public static PeriodDuration

Returns:

the equivalent duration, not null
from
(TemporalAmount
the temporal amount to convert, not null
amount
)

Obtains an instance from a temporal amount.

public long

Returns:

the long value of the unit
get
(TemporalUnit
the TemporalUnit for which to return the value
unit
)

Implements java.time.temporal.TemporalAmount.get.

Gets the value of the requested unit.

public Duration

Returns:

the duration part
getDuration
()

Gets the duration part.

public Period

Returns:

the period part
getPeriod
()

Gets the period part.

public List<TemporalUnit>

Returns:

a list containing the days unit, not null
getUnits
()

Implements java.time.temporal.TemporalAmount.getUnits.

Gets the set of units supported by this amount.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this amount.

public boolean

Returns:

true if this period is zero-length
isZero
()

Checks if all parts of this amount are zero.

public PeriodDuration

Returns:

a Days based on this instance with the requested amount subtracted, not null
minus
(TemporalAmount
the 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 null
multipliedBy
(int
the scalar to multiply by, not null
scalar
)

Returns an instance with the amount multiplied by the specified scalar.

public PeriodDuration

Returns:

the negated amount, not null
negated
()

Returns an instance with the amount negated.

public PeriodDuration

Returns:

a PeriodDuration based on this one with excess duration normalized to days, not null
normalizedStandardDays
()

Returns a copy of this instance with the days and duration normalized using the standard day of 24 hours.

public PeriodDuration

Returns:

a PeriodDuration based on this one with excess months normalized to years, not null
normalizedYears
()

Returns a copy of this instance with the years and months exactly normalized.

public static PeriodDuration

Returns:

the combined period-duration, not null
of
(Period
the period, not null
period
,
Duration
the duration, not null
duration
)

Obtains an instance based on a period and duration.

public static PeriodDuration

Returns:

the combined period-duration, not null
of
(Period
the period, not null
period
)

Obtains an instance based on a period.

public static PeriodDuration

Returns:

the combined period-duration, not null
of
(Duration
the duration, not null
duration
)

Obtains an instance based on a duration.

public static PeriodDuration

Returns:

the parsed period, not null
parse
(CharSequence
the text to parse, not null
text
)

Obtains an instance from a text string such as PnYnMnDTnHnMnS.

public PeriodDuration

Returns:

a Days based on this instance with the requested amount added, not null
plus
(TemporalAmount
the amount to add, not null
amountToAdd
)

Returns a copy of this amount with the specified amount added.

private Object

Returns:

the singleton instance
readResolve
()

Resolves singletons.

public Temporal

Returns:

an object of the same type with the adjustment made, not null
subtractFrom
(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.temporal.TemporalAmount.subtractFrom.

Subtracts this amount from the specified temporal object.

public String

Returns:

the period in ISO-8601 string format
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the amount.

public PeriodDuration

Returns:

the updated period-duration
withDuration
(Duration
the new duration
duration
)

Returns a copy of this period-duration with a different duration.

public PeriodDuration

Returns:

the updated period-duration
withPeriod
(Period
the new period
period
)

Returns a copy of this period-duration with a different period.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

durationback to summary
private final Duration duration

The duration.

periodback to summary
private final Period period

The period.

SECONDS_PER_DAYback to summary
private static final long SECONDS_PER_DAY

The number of seconds per day.

serialVersionUIDback to summary
private static final long serialVersionUID

A serialization identifier for this class.

SUPPORTED_UNITSback to summary
private static final List<TemporalUnit> SUPPORTED_UNITS

The supported units.

ZEROback to summary
public static final PeriodDuration ZERO

A constant for a duration of zero.

Constructor Detail

PeriodDurationback to summary
private PeriodDuration(Period period, Duration duration)

Constructs an instance.

Parameters
period:Period

the period

duration:Duration

the duration

Method Detail

addToback to summary
public Temporal addTo(Temporal temporal)

Implements java.time.temporal.TemporalAmount.addTo.

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.

Parameters
temporal:Temporal

the temporal object to adjust, not null

Returns:Temporal

an object of the same type with the adjustment made, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to add
UnsupportedTemporalTypeException:
if the DAYS unit is not supported
ArithmeticException:
if numeric overflow occurs
betweenback 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 LocalDate and LocalTime. If the time is missing, it will be defaulted to midnight. If one date is missing, it will be defaulted to the other date. It then finds the amount of time between the two dates and between the two times.

Parameters
startInclusive:Temporal

the start, inclusive, not null

endExclusive:Temporal

the end, exclusive, not null

Returns:PeriodDuration

the number of days between this date and the end date, not null

equalsback to summary
public boolean equals(Object otherAmount)

Overrides java.lang.Object.equals.

Checks if this amount is equal to the specified PeriodDuration.

The comparison is based on the underlying period and duration.

Parameters
otherAmount:Object

the other amount, null returns false

Returns:boolean

true if the other amount is equal to this one

Annotations
@Override
fromback 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 TemporalAmount represents an amount of time which this factory extracts to a PeriodDuration.

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 Duration#plus(Duration). If the unit is days or weeks, it will be totalled into the days part of the period. If the unit is months or quarters, it will be totalled into the months part of the period. If the unit is years, decades, centuries or millennia, it will be totalled into the years part of the period.

Parameters
amount:TemporalAmount

the temporal amount to convert, not null

Returns:PeriodDuration

the equivalent duration, not null

Exceptions
DateTimeException:
if unable to convert to a Duration
ArithmeticException:
if numeric overflow occurs
getback to summary
public long get(TemporalUnit unit)

Implements java.time.temporal.TemporalAmount.get.

Gets the value of the requested unit.

This returns a value for the supported units - ChronoUnit#YEARS, ChronoUnit#MONTHS, ChronoUnit#DAYS, ChronoUnit#SECONDS and ChronoUnit#NANOS. All other units throw an exception. Note that hours and minutes throw an exception.

Parameters
unit:TemporalUnit

the TemporalUnit for which to return the value

Returns:long

the long value of the unit

Annotations
@Override
Exceptions
UnsupportedTemporalTypeException:
if the unit is not supported
getDurationback to summary
public Duration getDuration()

Gets the duration part.

Returns:Duration

the duration part

getPeriodback to summary
public Period getPeriod()

Gets the period part.

Returns:Period

the period part

getUnitsback to summary
public List<TemporalUnit> getUnits()

Implements java.time.temporal.TemporalAmount.getUnits.

Gets the set of units supported by this amount.

This returns the list ChronoUnit#YEARS, ChronoUnit#MONTHS, ChronoUnit#DAYS, ChronoUnit#SECONDS and ChronoUnit#NANOS.

This set can be used in conjunction with get(TemporalUnit) to access the entire state of the amount.

Returns:List<TemporalUnit>

a list containing the days unit, not null

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

A hash code for this amount.

Returns:int

a suitable hash code

Annotations
@Override
isZeroback to summary
public boolean isZero()

Checks if all parts of this amount are zero.

This returns true if both Period#isZero() and Duration#isZero() return true.

Returns:boolean

true if this period is zero-length

minusback to summary
public PeriodDuration minus(TemporalAmount amountToAdd)

Returns a copy of this amount with the specified amount subtracted.

The parameter is converted using PeriodDuration#from(TemporalAmount). The period and duration are combined separately.

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:TemporalAmount

the amount to add, not null

Returns:PeriodDuration

a Days based on this instance with the requested amount subtracted, not null

Exceptions
DateTimeException:
if the specified amount contains an invalid unit
ArithmeticException:
if numeric overflow occurs
multipliedByback 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.

Parameters
scalar:int

the scalar to multiply by, not null

Returns:PeriodDuration

the amount multiplied by the specified scalar, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
negatedback to summary
public PeriodDuration negated()

Returns an instance with the amount negated.

This instance is immutable and unaffected by this method call.

Returns:PeriodDuration

the negated amount, not null

Exceptions
ArithmeticException:
if numeric overflow occurs, which only happens if the amount is Long.MIN_VALUE
normalizedStandardDaysback 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.

Returns:PeriodDuration

a PeriodDuration based on this one with excess duration normalized to days, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
normalizedYearsback 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.

Returns:PeriodDuration

a PeriodDuration based on this one with excess months normalized to years, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
ofback 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.

Parameters
period:Period

the period, not null

duration:Duration

the duration, not null

Returns:PeriodDuration

the combined period-duration, not null

ofback to summary
public static PeriodDuration of(Period period)

Obtains an instance based on a period.

The duration will be zero.

Parameters
period:Period

the period, not null

Returns:PeriodDuration

the combined period-duration, not null

ofback to summary
public static PeriodDuration of(Duration duration)

Obtains an instance based on a duration.

The period will be zero.

Parameters
duration:Duration

the duration, not null

Returns:PeriodDuration

the combined period-duration, not null

parseback to summary
public static PeriodDuration parse(CharSequence text)

Obtains an instance from a text string such as PnYnMnDTnHnMnS.

This will parse the string produced by toString() which is based on the ISO-8601 period formats PnYnMnDTnHnMnS and PnW.

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 int. Any week-based input is multiplied by 7 and treated as a number of days.

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 PYYYY-MM-DDTHH:MM:SS is not supported.

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))
Parameters
text:CharSequence

the text to parse, not null

Returns:PeriodDuration

the parsed period, not null

Annotations
@FromString
Exceptions
DateTimeParseException:
if the text cannot be parsed to a period
plusback to summary
public PeriodDuration plus(TemporalAmount amountToAdd)

Returns a copy of this amount with the specified amount added.

The parameter is converted using PeriodDuration#from(TemporalAmount). The period and duration are combined separately.

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:TemporalAmount

the amount to add, not null

Returns:PeriodDuration

a Days based on this instance with the requested amount added, not null

Exceptions
DateTimeException:
if the specified amount contains an invalid unit
ArithmeticException:
if numeric overflow occurs
readResolveback to summary
private Object readResolve()

Resolves singletons.

Returns:Object

the singleton instance

subtractFromback to summary
public Temporal subtractFrom(Temporal temporal)

Implements java.time.temporal.TemporalAmount.subtractFrom.

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.

Parameters
temporal:Temporal

the temporal object to adjust, not null

Returns:Temporal

an object of the same type with the adjustment made, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to subtract
UnsupportedTemporalTypeException:
if the DAYS unit is not supported
ArithmeticException:
if numeric overflow occurs
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

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

Returns:String

the period in ISO-8601 string format

Annotations
@Override
@ToString
withDurationback 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.

Parameters
duration:Duration

the new duration

Returns:PeriodDuration

the updated period-duration

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

Parameters
period:Period

the new period

Returns:PeriodDuration

the updated period-duration