public interface ChronoPeriod extends TemporalAmount
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.
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.
Modifier and Type | Method and Description |
---|---|
Temporal |
addTo(Temporal temporal)
Adds this period to the specified temporal object.
|
boolean |
equals(Object obj)
Checks if this period is equal to another period, including the chronology.
|
long |
get(TemporalUnit unit)
Gets the value of the requested unit.
|
Chronology |
getChronology()
Gets the chronology that defines the meaning of the supported units.
|
List<TemporalUnit> |
getUnits()
Gets the set of units supported by this period.
|
int |
hashCode()
A hash code for this period.
|
boolean |
isNegative()
Checks if any of the supported units of this period are negative.
|
boolean |
isZero()
Checks if all the supported units of this period are zero.
|
ChronoPeriod |
minus(TemporalAmount amountToSubtract)
Returns a copy of this period with the specified period subtracted.
|
ChronoPeriod |
multipliedBy(int scalar)
Returns a new instance with each amount in this period in this period
multiplied by the specified scalar.
|
ChronoPeriod |
negated()
Returns a new instance with each amount in this period negated.
|
ChronoPeriod |
normalized()
Returns a copy of this period with the amounts of each unit normalized.
|
ChronoPeriod |
plus(TemporalAmount amountToAdd)
Returns a copy of this period with the specified period added.
|
Temporal |
subtractFrom(Temporal temporal)
Subtracts this period from the specified temporal object.
|
String |
toString()
Outputs this period as a
String . |
Temporal addTo(Temporal temporal)
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.
addTo
in interface TemporalAmount
temporal
- the temporal object to adjust, not nullDateTimeException
- if unable to addboolean equals(@Nullable Object obj)
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".
equals
in class Object
obj
- the object to check, null returns falseObject.hashCode()
,
HashMap
long get(TemporalUnit unit)
The supported units are chronology specific.
They will typically be YEARS
,
MONTHS
and DAYS
.
Requesting an unsupported unit will throw an exception.
get
in interface TemporalAmount
unit
- the TemporalUnit
for which to return the valueDateTimeException
- if the unit is not supportedUnsupportedTemporalTypeException
- if the unit is not supportedChronology getChronology()
The period is defined by the chronology.
It controls the supported units and restricts addition/subtraction
to ChronoLocalDate
instances of the same chronology.
List<TemporalUnit> getUnits()
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.
getUnits
in interface TemporalAmount
int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
boolean isNegative()
boolean isZero()
ChronoPeriod minus(TemporalAmount amountToSubtract)
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.
amountToSubtract
- the period to subtract, not nullChronoPeriod
based on this period with the requested period subtracted, not nullChronoPeriod multipliedBy(int 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.
scalar
- the scalar to multiply by, not nullChronoPeriod
based on this period with the amounts multiplied
by the scalar, not nullChronoPeriod 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.
ChronoPeriod
based on this period with the amounts negated, not nullChronoPeriod 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.
ChronoPeriod
based on this period with the amounts of each
unit normalized, not nullChronoPeriod plus(TemporalAmount amountToAdd)
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.
amountToAdd
- the period to add, not nullChronoPeriod
based on this period with the requested period added, not nullTemporal subtractFrom(Temporal temporal)
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.
subtractFrom
in interface TemporalAmount
temporal
- the temporal object to adjust, not nullDateTimeException
- if unable to subtract