Top Description Methods
java.time.chrono

public Interface ChronoPeriod

extends TemporalAmount
Known Direct Implementers
java.time.chrono.ChronoPeriodImpl, java.time.Period
Imports
java.time.DateTimeException, java.time.temporal.ChronoUnit, .Temporal, .TemporalAmount, .TemporalUnit, .UnsupportedTemporalTypeException, java.util.List, .Objects

A date-based amount of time, such as '3 years, 4 months and 5 days' in an arbitrary chronology, intended for advanced globalization use cases.

This interface models a date-based amount of time in a calendar system. While most calendar systems use years, months and days, some do not. Therefore, this interface operates solely in terms of a set of supported units that are defined by the Chronology. The set of supported units is fixed for a given chronology. The amount of a supported unit may be set to zero.

The period is modeled as a directed amount of time, meaning that individual parts of the period may be negative.

Implementation Specification

This interface must be implemented with care to ensure other classes operate correctly. All implementations that can be instantiated must be final, immutable and thread-safe. Subclasses should be Serializable wherever possible.

Since
1.8

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
)

Redeclares java.time.temporal.TemporalAmount.addTo.

Adds this period to the specified temporal object.

public static ChronoPeriod

Returns:

the period between this date and the end date, not null
between
(ChronoLocalDate
the start date, inclusive, specifying the chronology of the calculation, not null
startDateInclusive
,
ChronoLocalDate
the end date, exclusive, in any chronology, not null
endDateExclusive
)

Obtains a ChronoPeriod consisting of amount of time between two dates.

public boolean

Returns:

true if this is equal to the other period
equals
(Object
the object to check, null returns false
obj
)

Checks if this period is equal to another period, including the chronology.

public long

Returns:

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

Redeclares java.time.temporal.TemporalAmount.get.

Gets the value of the requested unit.

public Chronology

Returns:

the chronology defining the period, not null
getChronology
()

Gets the chronology that defines the meaning of the supported units.

public List<TemporalUnit>

Returns:

a list containing the supported units, not null
getUnits
()

Redeclares java.time.temporal.TemporalAmount.getUnits.

Gets the set of units supported by this period.

public int

Returns:

a suitable hash code
hashCode
()

A hash code for this period.

public default boolean

Returns:

true if any unit of this period is negative
isNegative
()

Checks if any of the supported units of this period are negative.

public default boolean

Returns:

true if this period is zero-length
isZero
()

Checks if all the supported units of this period are zero.

public ChronoPeriod

Returns:

a ChronoPeriod based on this period with the requested period subtracted, not null
minus
(TemporalAmount
the period to subtract, not null
amountToSubtract
)

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

public ChronoPeriod

Returns:

a ChronoPeriod based on this period with the amounts multiplied by the scalar, not null
multipliedBy
(int
the scalar to multiply by, not null
scalar
)

Returns a new instance with each amount in this period in this period multiplied by the specified scalar.

public default ChronoPeriod

Returns:

a ChronoPeriod based on this period with the amounts negated, not null
negated
()

Returns a new instance with each amount in this period negated.

public ChronoPeriod

Returns:

a ChronoPeriod based on this period with the amounts of each unit normalized, not null
normalized
()

Returns a copy of this period with the amounts of each unit normalized.

public ChronoPeriod

Returns:

a ChronoPeriod based on this period with the requested period added, not null
plus
(TemporalAmount
the period to add, not null
amountToAdd
)

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

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
)

Redeclares java.time.temporal.TemporalAmount.subtractFrom.

Subtracts this period from the specified temporal object.

public String

Returns:

a string representation of this period, not null
toString
()

Outputs this period as a String.

Method Detail

addToback to summary
public Temporal addTo(Temporal temporal)

Redeclares java.time.temporal.TemporalAmount.addTo.

Adds this period to the specified temporal object.

This returns a temporal object of the same observable type as the input with this period added.

In most cases, it is clearer to reverse the calling pattern by using Temporal#plus(TemporalAmount).

  // these two lines are equivalent, but the second approach is recommended
  dateTime = thisPeriod.addTo(dateTime);
  dateTime = dateTime.plus(thisPeriod);

The specified temporal must have the same chronology as this period. This returns a temporal with the non-zero supported units added.

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
ArithmeticException:
if numeric overflow occurs
betweenback to summary
public static ChronoPeriod between(ChronoLocalDate startDateInclusive, ChronoLocalDate endDateExclusive)

Obtains a ChronoPeriod consisting of amount of time between two dates.

The start date is included, but the end date is not. The period is calculated using ChronoLocalDate#until(ChronoLocalDate). As such, the calculation is chronology specific.

The chronology of the first date is used. The chronology of the second date is ignored, with the date being converted to the target chronology system before the calculation starts.

The result of this method can be a negative period if the end is before the start. In most cases, the positive/negative sign will be the same in each of the supported fields.

Parameters
startDateInclusive:ChronoLocalDate

the start date, inclusive, specifying the chronology of the calculation, not null

endDateExclusive:ChronoLocalDate

the end date, exclusive, in any chronology, not null

Returns:ChronoPeriod

the period between this date and the end date, not null

See Also
ChronoLocalDate#until(ChronoLocalDate)
equalsback to summary
public boolean equals(Object obj)

Checks if this period is equal to another period, including the chronology.

Compares this period with another ensuring that the type, each amount and the chronology are the same. Note that this means that a period of "15 Months" is not equal to a period of "1 Year and 3 Months".

Parameters
obj:Object

the object to check, null returns false

Returns:boolean

true if this is equal to the other period

Annotations
@Override
getback to summary
public long get(TemporalUnit unit)

Redeclares java.time.temporal.TemporalAmount.get.

Gets the value of the requested unit.

The supported units are chronology specific. They will typically be YEARS, MONTHS and DAYS. Requesting an unsupported unit will 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
DateTimeException:
if the unit is not supported
UnsupportedTemporalTypeException:
if the unit is not supported
getChronologyback to summary
public Chronology getChronology()

Gets the chronology that defines the meaning of the supported units.

The period is defined by the chronology. It controls the supported units and restricts addition/subtraction to ChronoLocalDate instances of the same chronology.

Returns:Chronology

the chronology defining the period, not null

getUnitsback to summary
public List<TemporalUnit> getUnits()

Redeclares java.time.temporal.TemporalAmount.getUnits.

Gets the set of units supported by this period.

The supported units are chronology specific. They will typically be YEARS, MONTHS and DAYS. They are returned in order from largest to smallest.

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

Returns:List<TemporalUnit>

a list containing the supported units, not null

Annotations
@Override
hashCodeback to summary
public int hashCode()

A hash code for this period.

Returns:int

a suitable hash code

Annotations
@Override
isNegativeback to summary
public default boolean isNegative()

Checks if any of the supported units of this period are negative.

Returns:boolean

true if any unit of this period is negative

isZeroback to summary
public default boolean isZero()

Checks if all the supported units of this period are zero.

Returns:boolean

true if this period is zero-length

minusback to summary
public ChronoPeriod minus(TemporalAmount amountToSubtract)

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

If the specified amount is a ChronoPeriod then it must have the same chronology as this period. Implementations may choose to accept or reject other TemporalAmount implementations.

This instance is immutable and unaffected by this method call.

Parameters
amountToSubtract:TemporalAmount

the period to subtract, not null

Returns:ChronoPeriod

a ChronoPeriod based on this period with the requested period subtracted, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
multipliedByback to summary
public ChronoPeriod multipliedBy(int scalar)

Returns a new instance with each amount in this period in this period multiplied by the specified scalar.

This returns a period with each supported unit individually multiplied. For example, a period of "2 years, -3 months and 4 days" multiplied by 3 will return "6 years, -9 months and 12 days". No normalization is performed.

Parameters
scalar:int

the scalar to multiply by, not null

Returns:ChronoPeriod

a ChronoPeriod based on this period with the amounts multiplied by the scalar, not null

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

Returns a new instance with each amount in this period negated.

This returns a period with each supported unit individually negated. For example, a period of "2 years, -3 months and 4 days" will be negated to "-2 years, 3 months and -4 days". No normalization is performed.

Returns:ChronoPeriod

a ChronoPeriod based on this period with the amounts negated, not null

Exceptions
ArithmeticException:
if numeric overflow occurs, which only happens if one of the units has the value Long.MIN_VALUE
normalizedback to summary
public ChronoPeriod normalized()

Returns a copy of this period with the amounts of each unit normalized.

The process of normalization is specific to each calendar system. For example, in the ISO calendar system, the years and months are normalized but the days are not, such that "15 months" would be normalized to "1 year and 3 months".

This instance is immutable and unaffected by this method call.

Returns:ChronoPeriod

a ChronoPeriod based on this period with the amounts of each unit normalized, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
plusback to summary
public ChronoPeriod plus(TemporalAmount amountToAdd)

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

If the specified amount is a ChronoPeriod then it must have the same chronology as this period. Implementations may choose to accept or reject other TemporalAmount implementations.

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:TemporalAmount

the period to add, not null

Returns:ChronoPeriod

a ChronoPeriod based on this period with the requested period added, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
subtractFromback to summary
public Temporal subtractFrom(Temporal temporal)

Redeclares java.time.temporal.TemporalAmount.subtractFrom.

Subtracts this period from the specified temporal object.

This returns a temporal object of the same observable type as the input with this period subtracted.

In most cases, it is clearer to reverse the calling pattern by using Temporal#minus(TemporalAmount).

  // these two lines are equivalent, but the second approach is recommended
  dateTime = thisPeriod.subtractFrom(dateTime);
  dateTime = dateTime.minus(thisPeriod);

The specified temporal must have the same chronology as this period. This returns a temporal with the non-zero supported units subtracted.

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
ArithmeticException:
if numeric overflow occurs
toStringback to summary
public String toString()

Outputs this period as a String.

The output will include the period amounts and chronology.

Returns:String

a string representation of this period, not null

Annotations
@Override