Top Description Fields Constructors Methods
java.time.chrono

pack-priv final Class ChronoPeriodImpl

extends Object
implements ChronoPeriod, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.time.chrono.ChronoPeriod, java.time.temporal.TemporalAmount
Static Imports
java.time.temporal.ChronoField.MONTH_OF_YEAR, .ChronoUnit.DAYS, .ChronoUnit.MONTHS, .ChronoUnit.YEARS

A period expressed in terms of a standard year-month-day calendar system.

This class is used by applications seeking to handle dates in non-ISO calendar systems. For example, the Japanese, Minguo, Thai Buddhist and others.

Implementation Specification

This class is immutable nad thread-safe.

Since
1.8

Field Summary

Modifier and TypeField and Description
private final Chronology
chrono

The chronology.

pack-priv final int
days

The number of days.

pack-priv final int
months

The number of months.

private static final long
serialVersionUID

Serialization version.

private static final List<TemporalUnit>
SUPPORTED_UNITS

The set of supported units.

pack-priv final int
years

The number of years.

Constructor Summary

AccessConstructor and Description
pack-priv
ChronoPeriodImpl(Chronology chrono, int years, int months, int days)

Creates an instance.

Method Summary

Modifier and TypeMethod and Description
public Temporal
addTo(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.chrono.ChronoPeriod.addTo.

Adds this period to the specified temporal object.

public boolean
equals(Object
the object to check, null returns false
obj
)

Overrides java.lang.Object.equals.

Implements java.time.chrono.ChronoPeriod.equals.

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

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

Implements java.time.chrono.ChronoPeriod.get.

Gets the value of the requested unit.

public Chronology
getChronology()

Implements java.time.chrono.ChronoPeriod.getChronology.

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

public List<TemporalUnit>
getUnits()

Implements java.time.chrono.ChronoPeriod.getUnits.

Gets the set of units supported by this period.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.time.chrono.ChronoPeriod.hashCode.

A hash code for this period.

public boolean
isNegative()

Overrides default java.time.chrono.ChronoPeriod.isNegative.

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

public boolean
isZero()

Overrides default java.time.chrono.ChronoPeriod.isZero.

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

public ChronoPeriod
minus(TemporalAmount
the period to subtract, not null
amountToSubtract
)

Implements java.time.chrono.ChronoPeriod.minus.

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

private long

Returns:

the month range, -1 if not fixed range
monthRange
()

Calculates the range of months.

public ChronoPeriod
multipliedBy(int
the scalar to multiply by, not null
scalar
)

Implements java.time.chrono.ChronoPeriod.multipliedBy.

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

public ChronoPeriod
normalized()

Implements java.time.chrono.ChronoPeriod.normalized.

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

public ChronoPeriod
plus(TemporalAmount
the period to add, not null
amountToAdd
)

Implements java.time.chrono.ChronoPeriod.plus.

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

pack-priv static ChronoPeriodImpl
private void
readObject(ObjectInputStream
the stream to read
s
)

Defend against malicious streams.

public Temporal
subtractFrom(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.chrono.ChronoPeriod.subtractFrom.

Subtracts this period from the specified temporal object.

public String
toString()

Overrides java.lang.Object.toString.

Implements java.time.chrono.ChronoPeriod.toString.

Outputs this period as a String.

private ChronoPeriodImpl

Returns:

the period, not null
validateAmount
(TemporalAmount
the temporal amount to convert, not null
amount
)

Obtains an instance of ChronoPeriodImpl from a temporal amount.

private void
validateChrono(TemporalAccessor temporal)

Validates that the temporal has the correct chronology.

pack-priv void
protected Object

Returns:

the instance of Ser, not null
writeReplace
()

Writes the Chronology using a dedicated serialized form.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

chronoback to summary
private final Chronology chrono

The chronology.

Annotations
@SuppressWarnings:serial
daysback to summary
pack-priv final int days

The number of days.

monthsback to summary
pack-priv final int months

The number of months.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

Annotations
@Serial
SUPPORTED_UNITSback to summary
private static final List<TemporalUnit> SUPPORTED_UNITS

The set of supported units.

yearsback to summary
pack-priv final int years

The number of years.

Constructor Detail

ChronoPeriodImplback to summary
pack-priv ChronoPeriodImpl(Chronology chrono, int years, int months, int days)

Creates an instance.

Method Detail

addToback to summary
public Temporal addTo(Temporal temporal)

Implements java.time.chrono.ChronoPeriod.addTo.

Doc from java.time.chrono.ChronoPeriod.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
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Implements java.time.chrono.ChronoPeriod.equals.

Doc from java.time.chrono.ChronoPeriod.equals.

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)

Implements java.time.chrono.ChronoPeriod.get.

Doc from java.time.chrono.ChronoPeriod.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
getChronologyback to summary
public Chronology getChronology()

Implements java.time.chrono.ChronoPeriod.getChronology.

Doc from java.time.chrono.ChronoPeriod.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

Annotations
@Override
getUnitsback to summary
public List<TemporalUnit> getUnits()

Implements java.time.chrono.ChronoPeriod.getUnits.

Doc from java.time.chrono.ChronoPeriod.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()

Overrides java.lang.Object.hashCode.

Implements java.time.chrono.ChronoPeriod.hashCode.

Doc from java.time.chrono.ChronoPeriod.hashCode.

A hash code for this period.

Returns:int

a suitable hash code

Annotations
@Override
isNegativeback to summary
public boolean isNegative()

Overrides default java.time.chrono.ChronoPeriod.isNegative.

Doc from java.time.chrono.ChronoPeriod.isNegative.

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

Returns:boolean

true if any unit of this period is negative

Annotations
@Override
isZeroback to summary
public boolean isZero()

Overrides default java.time.chrono.ChronoPeriod.isZero.

Doc from java.time.chrono.ChronoPeriod.isZero.

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

Returns:boolean

true if this period is zero-length

Annotations
@Override
minusback to summary
public ChronoPeriod minus(TemporalAmount amountToSubtract)

Implements java.time.chrono.ChronoPeriod.minus.

Doc from java.time.chrono.ChronoPeriod.minus.

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

Annotations
@Override
monthRangeback to summary
private long monthRange()

Calculates the range of months.

Returns:long

the month range, -1 if not fixed range

multipliedByback to summary
public ChronoPeriod multipliedBy(int scalar)

Implements java.time.chrono.ChronoPeriod.multipliedBy.

Doc from java.time.chrono.ChronoPeriod.multipliedBy.

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

Annotations
@Override
normalizedback to summary
public ChronoPeriod normalized()

Implements java.time.chrono.ChronoPeriod.normalized.

Doc from java.time.chrono.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

Annotations
@Override
plusback to summary
public ChronoPeriod plus(TemporalAmount amountToAdd)

Implements java.time.chrono.ChronoPeriod.plus.

Doc from java.time.chrono.ChronoPeriod.plus.

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

Annotations
@Override
readExternalback to summary
pack-priv static ChronoPeriodImpl readExternal(DataInput in) throws IOException
readObjectback to summary
private void readObject(ObjectInputStream s) throws ObjectStreamException

Defend against malicious streams.

Parameters
s:ObjectInputStream

the stream to read

Annotations
@Serial
Exceptions
InvalidObjectException:
always
subtractFromback to summary
public Temporal subtractFrom(Temporal temporal)

Implements java.time.chrono.ChronoPeriod.subtractFrom.

Doc from java.time.chrono.ChronoPeriod.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
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements java.time.chrono.ChronoPeriod.toString.

Doc from java.time.chrono.ChronoPeriod.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
validateAmountback to summary
private ChronoPeriodImpl validateAmount(TemporalAmount amount)

Obtains an instance of ChronoPeriodImpl from a temporal amount.

Parameters
amount:TemporalAmount

the temporal amount to convert, not null

Returns:ChronoPeriodImpl

the period, not null

validateChronoback to summary
private void validateChrono(TemporalAccessor temporal)

Validates that the temporal has the correct chronology.

writeExternalback to summary
pack-priv void writeExternal(DataOutput out) throws IOException
writeReplaceback to summary
protected Object writeReplace()

Writes the Chronology using a dedicated serialized form.

 out.writeByte(12);  // identifies this as a ChronoPeriodImpl
 out.writeUTF(getId());  // the chronology
 out.writeInt(years);
 out.writeInt(months);
 out.writeInt(days);
Returns:Object

the instance of Ser, not null

Annotations
@Serial