APIdia Launch Partner
Top Description Fields Constructors Methods
java.time

public final Class Period

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

A date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'.

This class models a quantity or amount of time in terms of years, months and days. See Duration for the time-based equivalent to this class.

Durations and periods differ in their treatment of daylight savings time when added to ZonedDateTime. A Duration will add an exact number of seconds, thus a duration of one day is always exactly 24 hours. By contrast, a Period will add a conceptual day, trying to maintain the local time.

For example, consider adding a period of one day and a duration of one day to 18:00 on the evening before a daylight savings gap. The Period will add the conceptual day and result in a ZonedDateTime at 18:00 the following day. By contrast, the Duration will add exactly 24 hours, resulting in a ZonedDateTime at 19:00 the following day (assuming a one hour DST gap).

The supported units of a period are YEARS, MONTHS and DAYS. All three fields are always present, but may be set to zero.

The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time.

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

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. The equals method should be used for comparisons.

Implementation Specification

This class is immutable and thread-safe.

Since
1.8

Field Summary

Modifier and TypeField and Description
private final int
private final int
private static final Pattern
PATTERN

The pattern for parsing.

private static final long
serialVersionUID

Serialization version.

private static final List<TemporalUnit>
SUPPORTED_UNITS

The set of supported units.

private final int
public static final Period
ZERO

A constant for a period of zero.

Constructor Summary

AccessConstructor and Description
private
Period(int
the amount
years
,
int
the amount
months
,
int
the amount
days
)

Constructor.

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.chrono.ChronoPeriod.addTo.

Adds this period to the specified temporal object.

public static Period

Returns:

the period between this date and the end date, not null
between
(LocalDate
the start date, inclusive, not null
startDateInclusive
,
LocalDate
the end date, exclusive, not null
endDateExclusive
)

Obtains a Period consisting of the number of years, months, and days between two dates.

private static boolean
charMatch(CharSequence text, int start, int end, char c)

private static Period
create(int
the amount
years
,
int
the amount
months
,
int
the amount
days
)

Creates an instance.

public boolean

Returns:

true if this is equal to the other period
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.

public static Period

Returns:

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

Obtains an instance of Period 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.chrono.ChronoPeriod.get.

Gets the value of the requested unit.

public IsoChronology

Returns:

the ISO chronology, not null
getChronology
()

Implements java.time.chrono.ChronoPeriod.getChronology.

Gets the chronology of this period, which is the ISO calendar system.

public int

Returns:

the amount of days of this period, may be negative
getDays
()

Gets the amount of days of this period.

public int

Returns:

the amount of months of this period, may be negative
getMonths
()

Gets the amount of months of this period.

public List<TemporalUnit>

Returns:

a list containing the years, months and days units, not null
getUnits
()

Implements java.time.chrono.ChronoPeriod.getUnits.

Gets the set of units supported by this period.

public int

Returns:

the amount of years of this period, may be negative
getYears
()

Gets the amount of years of this period.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.time.chrono.ChronoPeriod.hashCode.

A hash code for this period.

public boolean

Returns:

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

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

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

public boolean

Returns:

true if this period is zero-length
isZero
()

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

Checks if all three units of this period are zero.

public Period

Returns:

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

Implements java.time.chrono.ChronoPeriod.minus.

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

public Period

Returns:

a Period based on this period with the specified days subtracted, not null
minusDays
(long
the days to subtract, positive or negative
daysToSubtract
)

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

public Period

Returns:

a Period based on this period with the specified months subtracted, not null
minusMonths
(long
the years to subtract, positive or negative
monthsToSubtract
)

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

public Period

Returns:

a Period based on this period with the specified years subtracted, not null
minusYears
(long
the years to subtract, positive or negative
yearsToSubtract
)

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

public Period

Returns:

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

Implements java.time.chrono.ChronoPeriod.multipliedBy.

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

public Period

Returns:

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

Overrides default java.time.chrono.ChronoPeriod.negated.

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

public Period

Returns:

a Period based on this period with excess months normalized to years, not null
normalized
()

Implements java.time.chrono.ChronoPeriod.normalized.

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

public static Period

Returns:

the period of years, months and days, not null
of
(int
the amount of years, may be negative
years
,
int
the amount of months, may be negative
months
,
int
the amount of days, may be negative
days
)

Obtains a Period representing a number of years, months and days.

public static Period

Returns:

the period of days, not null
ofDays
(int
the number of days, positive or negative
days
)

Obtains a Period representing a number of days.

public static Period

Returns:

the period of months, not null
ofMonths
(int
the number of months, positive or negative
months
)

Obtains a Period representing a number of months.

public static Period

Returns:

the period, with the input weeks converted to days, not null
ofWeeks
(int
the number of weeks, positive or negative
weeks
)

Obtains a Period representing a number of weeks.

public static Period

Returns:

the period of years, not null
ofYears
(int
the number of years, positive or negative
years
)

Obtains a Period representing a number of years.

public static Period

Returns:

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

Obtains a Period from a text string such as PnYnMnD.

private static int
parseNumber(CharSequence text, int start, int end, int negate)

public Period

Returns:

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

Implements java.time.chrono.ChronoPeriod.plus.

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

public Period

Returns:

a Period based on this period with the specified days added, not null
plusDays
(long
the days to add, positive or negative
daysToAdd
)

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

public Period

Returns:

a Period based on this period with the specified months added, not null
plusMonths
(long
the months to add, positive or negative
monthsToAdd
)

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

public Period

Returns:

a Period based on this period with the specified years added, not null
plusYears
(long
the years to add, positive or negative
yearsToAdd
)

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

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

Defend against malicious streams.

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.chrono.ChronoPeriod.subtractFrom.

Subtracts this period from the specified temporal object.

public String

Returns:

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

Overrides java.lang.Object.toString.

Implements java.time.chrono.ChronoPeriod.toString.

Outputs this period as a String, such as P6Y3M1D.

public long

Returns:

the total number of months in the period, may be negative
toTotalMonths
()

Gets the total number of months in this period.

private void
validateChrono(TemporalAccessor temporal)

Validates that the temporal has the correct chronology.

public Period

Returns:

a Period based on this period with the requested days, not null
withDays
(int
the days to represent, may be negative
days
)

Returns a copy of this period with the specified amount of days.

public Period

Returns:

a Period based on this period with the requested months, not null
withMonths
(int
the months to represent, may be negative
months
)

Returns a copy of this period with the specified amount of months.

public Period

Returns:

a Period based on this period with the requested years, not null
withYears
(int
the years to represent, may be negative
years
)

Returns a copy of this period with the specified amount of years.

pack-priv void
private Object

Returns:

the instance of Ser, not null
writeReplace
()

Writes the object using a dedicated serialized form.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

daysback to summary
private final int days
monthsback to summary
private final int months
PATTERNback to summary
private static final Pattern PATTERN

The pattern for parsing.

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
private final int years
ZEROback to summary
public static final Period ZERO

A constant for a period of zero.

Constructor Detail

Periodback to summary
private Period(int years, int months, int days)

Constructor.

Parameters
years:int

the amount

months:int

the amount

days:int

the amount

Method Detail

addToback to summary
public Temporal addTo(Temporal temporal)

Implements 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. If the temporal has a chronology, it must be the ISO chronology.

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 calculation operates as follows. First, the chronology of the temporal is checked to ensure it is ISO chronology or null. Second, if the months are zero, the years are added if non-zero, otherwise the combination of years and months is added if non-zero. Finally, any days are added.

This approach ensures that a partial period can be added to a partial date. For example, a period of years and/or months can be added to a YearMonth, but a period including days cannot. The approach also adds years and months together when necessary, which ensures correct behaviour at the end of the month.

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 Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)

Obtains a Period consisting of the number of years, months, and days between two dates.

The start date is included, but the end date is not. The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign. The number of months is then split into years and months based on a 12 month year. A month is considered if the end day-of-month is greater than or equal to the start day-of-month. For example, from 2010-01-15 to 2011-03-18 is one year, two months and three days.

The result of this method can be a negative period if the end is before the start. The negative sign will be the same in each of year, month and day.

Parameters
startDateInclusive:LocalDate

the start date, inclusive, not null

endDateExclusive:LocalDate

the end date, exclusive, not null

Returns:Period

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

See Also
ChronoLocalDate#until(ChronoLocalDate)
charMatchback to summary
private static boolean charMatch(CharSequence text, int start, int end, char c)
createback to summary
private static Period create(int years, int months, int days)

Creates an instance.

Parameters
years:int

the amount

months:int

the amount

days:int

the amount

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Implements java.time.chrono.ChronoPeriod.equals.

Checks if this period is equal to another period.

The comparison is based on the type Period and each of the three amounts. To be equal, the years, months and days units must be individually equal. 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
fromback to summary
public static Period from(TemporalAmount amount)

Obtains an instance of Period from a temporal amount.

This obtains a period based on the specified amount. A TemporalAmount represents an amount of time, which may be date-based or time-based, which this factory extracts to a Period.

The conversion loops around the set of units from the amount and uses the YEARS, MONTHS and DAYS units to create a period. If any other units are found then an exception is thrown.

If the amount is a ChronoPeriod then it must use the ISO chronology.

Parameters
amount:TemporalAmount

the temporal amount to convert, not null

Returns:Period

the equivalent period, not null

Exceptions
DateTimeException:
if unable to convert to a Period
ArithmeticException:
if the amount of years, months or days exceeds an int
getback to summary
public long get(TemporalUnit unit)

Implements java.time.chrono.ChronoPeriod.get.

Gets the value of the requested unit.

This returns a value for each of the three supported units, YEARS, MONTHS and DAYS. All other units 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 IsoChronology getChronology()

Implements java.time.chrono.ChronoPeriod.getChronology.

Gets the chronology of this period, which is the ISO calendar system.

The Chronology represents the calendar system in use. The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time.

Returns:IsoChronology

the ISO chronology, not null

Annotations
@Override
getDaysback to summary
public int getDays()

Gets the amount of days of this period.

This returns the days unit.

Returns:int

the amount of days of this period, may be negative

getMonthsback to summary
public int getMonths()

Gets the amount of months of this period.

This returns the months unit.

The months unit is not automatically normalized with the years unit. This means that a period of "15 months" is different to a period of "1 year and 3 months".

Returns:int

the amount of months of this period, may be negative

getUnitsback to summary
public List<TemporalUnit> getUnits()

Implements java.time.chrono.ChronoPeriod.getUnits.

Gets the set of units supported by this period.

The supported units are YEARS, MONTHS and DAYS. They are returned in the order years, months, days.

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 years, months and days units, not null

Annotations
@Override
getYearsback to summary
public int getYears()

Gets the amount of years of this period.

This returns the years unit.

The months unit is not automatically normalized with the years unit. This means that a period of "15 months" is different to a period of "1 year and 3 months".

Returns:int

the amount of years of this period, may be negative

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

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

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

This checks whether the years, months or days units are less than zero.

Returns:boolean

true if any unit of this period is negative

isZeroback to summary
public boolean isZero()

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

Checks if all three units of this period are zero.

A zero period has the value zero for the years, months and days units.

Returns:boolean

true if this period is zero-length

minusback to summary
public Period minus(TemporalAmount amountToSubtract)

Implements java.time.chrono.ChronoPeriod.minus.

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

This operates separately on the years, months and days. No normalization is performed.

For example, "1 year, 6 months and 3 days" minus "2 years, 2 months and 2 days" returns "-1 years, 4 months and 1 day".

The specified amount is typically an instance of Period. Other types are interpreted using Period#from(TemporalAmount).

This instance is immutable and unaffected by this method call.

Parameters
amountToSubtract:TemporalAmount

the amount to subtract, not null

Returns:Period

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

Exceptions
DateTimeException:
if the specified amount has a non-ISO chronology or contains an invalid unit
ArithmeticException:
if numeric overflow occurs
minusDaysback to summary
public Period minusDays(long daysToSubtract)

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

This subtracts the amount from the days unit in a copy of this period. The years and months units are unaffected. For example, "1 year, 6 months and 3 days" minus 2 days returns "1 year, 6 months and 1 day".

This instance is immutable and unaffected by this method call.

Parameters
daysToSubtract:long

the days to subtract, positive or negative

Returns:Period

a Period based on this period with the specified days subtracted, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
minusMonthsback to summary
public Period minusMonths(long monthsToSubtract)

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

This subtracts the amount from the months unit in a copy of this period. The years and days units are unaffected. For example, "1 year, 6 months and 3 days" minus 2 months returns "1 year, 4 months and 3 days".

This instance is immutable and unaffected by this method call.

Parameters
monthsToSubtract:long

the years to subtract, positive or negative

Returns:Period

a Period based on this period with the specified months subtracted, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
minusYearsback to summary
public Period minusYears(long yearsToSubtract)

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

This subtracts the amount from the years unit in a copy of this period. The months and days units are unaffected. For example, "1 year, 6 months and 3 days" minus 2 years returns "-1 years, 6 months and 3 days".

This instance is immutable and unaffected by this method call.

Parameters
yearsToSubtract:long

the years to subtract, positive or negative

Returns:Period

a Period based on this period with the specified years subtracted, not null

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

Implements java.time.chrono.ChronoPeriod.multipliedBy.

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

This returns a period with each of the years, months and days units 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:Period

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

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

Overrides default java.time.chrono.ChronoPeriod.negated.

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

This returns a period with each of the years, months and days units 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:Period

a Period 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 Integer.MIN_VALUE
normalizedback to summary
public Period normalized()

Implements java.time.chrono.ChronoPeriod.normalized.

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

This normalizes the years and months units, leaving the days unit unchanged. The months unit is adjusted to have an absolute value less than 12, 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".

This instance is immutable and unaffected by this method call.

Returns:Period

a Period based on this period with excess months normalized to years, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
ofback to summary
public static Period of(int years, int months, int days)

Obtains a Period representing a number of years, months and days.

This creates an instance based on years, months and days.

Parameters
years:int

the amount of years, may be negative

months:int

the amount of months, may be negative

days:int

the amount of days, may be negative

Returns:Period

the period of years, months and days, not null

ofDaysback to summary
public static Period ofDays(int days)

Obtains a Period representing a number of days.

The resulting period will have the specified days. The years and months units will be zero.

Parameters
days:int

the number of days, positive or negative

Returns:Period

the period of days, not null

ofMonthsback to summary
public static Period ofMonths(int months)

Obtains a Period representing a number of months.

The resulting period will have the specified months. The years and days units will be zero.

Parameters
months:int

the number of months, positive or negative

Returns:Period

the period of months, not null

ofWeeksback to summary
public static Period ofWeeks(int weeks)

Obtains a Period representing a number of weeks.

The resulting period will be day-based, with the amount of days equal to the number of weeks multiplied by 7. The years and months units will be zero.

Parameters
weeks:int

the number of weeks, positive or negative

Returns:Period

the period, with the input weeks converted to days, not null

ofYearsback to summary
public static Period ofYears(int years)

Obtains a Period representing a number of years.

The resulting period will have the specified years. The months and days units will be zero.

Parameters
years:int

the number of years, positive or negative

Returns:Period

the period of years, not null

parseback to summary
public static Period parse(CharSequence text)

Obtains a Period from a text string such as PnYnMnD.

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

The string starts with an optional sign, denoted by the ASCII negative or positive symbol. If negative, the whole period is negated. The ASCII letter "P" is next in upper or lower case. There are then four sections, each consisting of a number and a suffix. At least one of the four sections must be present. The sections have suffixes in ASCII of "Y", "M", "W" and "D" for years, months, weeks and days, accepted in upper or lower case. 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.

The leading plus/minus sign, and negative values for other units are not part of the ISO-8601 standard. In addition, ISO-8601 does not permit mixing between the PnYnMnD and PnW formats. Any week-based input is multiplied by 7 and treated as a number of days.

For example, the following are valid inputs:

  "P2Y"             -- Period.ofYears(2)
  "P3M"             -- Period.ofMonths(3)
  "P4W"             -- Period.ofWeeks(4)
  "P5D"             -- Period.ofDays(5)
  "P1Y2M3D"         -- Period.of(1, 2, 3)
  "P1Y2M3W4D"       -- Period.of(1, 2, 25)
  "P-1Y2M"          -- Period.of(-1, 2, 0)
  "-P1Y2M"          -- Period.of(-1, -2, 0)
Parameters
text:CharSequence

the text to parse, not null

Returns:Period

the parsed period, not null

Exceptions
DateTimeParseException:
if the text cannot be parsed to a period
parseNumberback to summary
private static int parseNumber(CharSequence text, int start, int end, int negate)
plusback to summary
public Period plus(TemporalAmount amountToAdd)

Implements java.time.chrono.ChronoPeriod.plus.

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

This operates separately on the years, months and days. No normalization is performed.

For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days" returns "3 years, 8 months and 5 days".

The specified amount is typically an instance of Period. Other types are interpreted using Period#from(TemporalAmount).

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:TemporalAmount

the amount to add, not null

Returns:Period

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

Exceptions
DateTimeException:
if the specified amount has a non-ISO chronology or contains an invalid unit
ArithmeticException:
if numeric overflow occurs
plusDaysback to summary
public Period plusDays(long daysToAdd)

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

This adds the amount to the days unit in a copy of this period. The years and months units are unaffected. For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6 months and 5 days".

This instance is immutable and unaffected by this method call.

Parameters
daysToAdd:long

the days to add, positive or negative

Returns:Period

a Period based on this period with the specified days added, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
plusMonthsback to summary
public Period plusMonths(long monthsToAdd)

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

This adds the amount to the months unit in a copy of this period. The years and days units are unaffected. For example, "1 year, 6 months and 3 days" plus 2 months returns "1 year, 8 months and 3 days".

This instance is immutable and unaffected by this method call.

Parameters
monthsToAdd:long

the months to add, positive or negative

Returns:Period

a Period based on this period with the specified months added, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
plusYearsback to summary
public Period plusYears(long yearsToAdd)

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

This adds the amount to the years unit in a copy of this period. The months and days units are unaffected. For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6 months and 3 days".

This instance is immutable and unaffected by this method call.

Parameters
yearsToAdd:long

the years to add, positive or negative

Returns:Period

a Period based on this period with the specified years added, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
readExternalback to summary
pack-priv static Period readExternal(DataInput in) throws IOException
readObjectback to summary
private void readObject(ObjectInputStream s) throws InvalidObjectException

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.

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. If the temporal has a chronology, it must be the ISO chronology.

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 calculation operates as follows. First, the chronology of the temporal is checked to ensure it is ISO chronology or null. Second, if the months are zero, the years are subtracted if non-zero, otherwise the combination of years and months is subtracted if non-zero. Finally, any days are subtracted.

This approach ensures that a partial period can be subtracted from a partial date. For example, a period of years and/or months can be subtracted from a YearMonth, but a period including days cannot. The approach also subtracts years and months together when necessary, which ensures correct behaviour at the end of the month.

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()

Overrides java.lang.Object.toString.

Implements java.time.chrono.ChronoPeriod.toString.

Outputs this period as a String, such as P6Y3M1D.

The output will be in the ISO-8601 period format. A zero period will be represented as zero days, 'P0D'.

Returns:String

a string representation of this period, not null

Annotations
@Override
toTotalMonthsback to summary
public long toTotalMonths()

Gets the total number of months in this period.

This returns the total number of months in the period by multiplying the number of years by 12 and adding the number of months.

This instance is immutable and unaffected by this method call.

Returns:long

the total number of months in the period, may be negative

validateChronoback to summary
private void validateChrono(TemporalAccessor temporal)

Validates that the temporal has the correct chronology.

withDaysback to summary
public Period withDays(int days)

Returns a copy of this period with the specified amount of days.

This sets the amount of the days unit in a copy of this period. The years and months units are unaffected.

This instance is immutable and unaffected by this method call.

Parameters
days:int

the days to represent, may be negative

Returns:Period

a Period based on this period with the requested days, not null

withMonthsback to summary
public Period withMonths(int months)

Returns a copy of this period with the specified amount of months.

This sets the amount of the months unit in a copy of this period. The years and days units are unaffected.

The months unit is not automatically normalized with the years unit. This means that a period of "15 months" is different to a period of "1 year and 3 months".

This instance is immutable and unaffected by this method call.

Parameters
months:int

the months to represent, may be negative

Returns:Period

a Period based on this period with the requested months, not null

withYearsback to summary
public Period withYears(int years)

Returns a copy of this period with the specified amount of years.

This sets the amount of the years unit in a copy of this period. The months and days units are unaffected.

The months unit is not automatically normalized with the years unit. This means that a period of "15 months" is different to a period of "1 year and 3 months".

This instance is immutable and unaffected by this method call.

Parameters
years:int

the years to represent, may be negative

Returns:Period

a Period based on this period with the requested years, not null

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

Writes the object using a dedicated serialized form.

Returns:Object

the instance of Ser, not null

Annotations
@Serial
Serial data
 out.writeByte(14);  // identifies a Period
 out.writeInt(years);
 out.writeInt(months);
 out.writeInt(days);