Home Reference Source
public class | source

Period

Extends:

TemporalAmount → Period

A date-based amount of time, 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 period 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 ChronoUnit#YEARS, ChronoUnit#MONTHS and ChronoUnit#DAYS. All three fields are always present, but may be set to zero.

The period may be used with any calendar system. The meaning of a 'year' or 'month' is only applied when the object is added to a date.

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

The months and years fields may be normalized (see normalized). The normalization assumes a 12 month year, so is not appropriate for all calendar systems.

Static properties of Class Period

Period.ZERO

A constant for a period of zero.

Static Method Summary

Static Public Methods
public static

between(startDate: LocalDate, endDate: LocalDate): Period

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

public static

create(years: number, months: number, days: number): Duration

Creates an instance.

public static

Obtains an instance of Period from a temporal amount.

public static

of(years: number, months: number, days: number): Period

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

public static

ofDays(days: number): Period

Obtains a Period representing a number of days.

public static

ofMonths(months: number): Period

Obtains a Period representing a number of months.

public static

ofWeeks(weeks: number): Period

Obtains a Period representing a number of weeks.

public static

ofYears(years: number): Period

Obtains a Period representing a number of years.

public static

parse(text: string): Period

Obtains a Period from a text string such as PnYnMnD.

Method Summary

Public Methods
public

addTo(temporal: Temporal): Temporal

Adds this period to the specified temporal object.

public

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

public

days(): number

Gets the amount of days of this period.

public

equals(obj: *): boolean

Checks if this period is equal to another period.

public

get(unit: TemporalUnit): number

Gets the value of the requested unit.

public

hashCode(): number

A hash code for this period.

public

isNegative(): boolean

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

public

isZero(): boolean

Checks if all three units of this period are zero.

public

minus(amountToSubtract: TemporalAmount): Period

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

public

minusDays(daysToSubtract: number): Period

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

public

minusMonths(monthsToSubtract: number): Period

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

public

minusYears(yearsToSubtract: number): Period

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

public

months(): number

Gets the amount of months of this period.

public

multipliedBy(scalar: number): Period

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

public

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

public

Returns a copy of this period with the years and months normalized using a 12 month year.

public

plus(amountToAdd: TemporalAmount): Period

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

public

plusDays(daysToAdd: number): Period

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

public

plusMonths(monthsToAdd: number): Period

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

public

plusYears(yearsToAdd: number): Period

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

public

Subtracts this period from the specified temporal object.

public

toJSON(): string

public

toString(): string

Outputs this period as a string, such as P6Y3M1D.

public

toTotalMonths(): number

Gets the total number of months in this period using a 12 month year.

public

Gets the list of units, from largest to smallest, that fully define this amount.

public

withDays(days: number): Period

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

public

withMonths(months: number): Period

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

public

withYears(years: number): Period

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

public

years(): number

Gets the amount of years of this period.

Inherited Summary

From class TemporalAmount
public

addTo(temporal: Temporal): Temporal

Adds to the specified temporal object.

public

get(unit: TemporalUnit): number

Returns the value of the requested unit.

public

Subtracts this object from the specified temporal object.

public

Returns the list of units uniquely defining the value of this TemporalAmount.

Static Public Methods

public static between(startDate: LocalDate, endDate: LocalDate): Period source

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.

see ChronoLocalDate.until

Params:

NameTypeAttributeDescription
startDate LocalDate

the start date, inclusive, not null

endDate LocalDate

the end date, exclusive, not null

Return:

Period

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

public static create(years: number, months: number, days: number): Duration source

Creates an instance.

Params:

NameTypeAttributeDescription
years number

the amount

months number

the amount

days number

the amount

Return:

Duration

public static from(amount: TemporalAmount): Period source

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 ChronoUnit#YEARS, ChronoUnit#MONTHS and ChronoUnit#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.

Params:

NameTypeAttributeDescription
amount TemporalAmount

the temporal amount to convert, not null

Return:

Period

the equivalent period, not null

Throw:

*

DateTimeException if unable to convert to a Period

*

ArithmeticException if the amount of years, months or days exceeds an int

public static of(years: number, months: number, days: number): Period source

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

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

Params:

NameTypeAttributeDescription
years number
  • nullable: false

the amount of years, may be negative

months number
  • nullable: false

the amount of months, may be negative

days number
  • nullable: false

the amount of days, may be negative

Return:

Period

the period of years, months and days, not null

public static ofDays(days: number): Period source

Obtains a Period representing a number of days.

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

Params:

NameTypeAttributeDescription
days number

the number of days, positive or negative

Return:

Period

the period of days, not null

public static ofMonths(months: number): Period source

Obtains a Period representing a number of months.

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

Params:

NameTypeAttributeDescription
months number

the number of months, positive or negative

Return:

Period

the period of months, not null

public static ofWeeks(weeks: number): Period source

Obtains a Period representing a number of weeks.

The resulting period will have days equal to the weeks multiplied by seven. The years and months units will be zero.

Params:

NameTypeAttributeDescription
weeks number

the number of weeks, positive or negative

Return:

Period

the period of days, not null

public static ofYears(years: number): Period source

Obtains a Period representing a number of years.

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

Params:

NameTypeAttributeDescription
years number

the number of years, positive or negative

Return:

Period

the period of years, not null

public static parse(text: string): Period source

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)

Params:

NameTypeAttributeDescription
text string

the text to parse, not null

Return:

Period

the parsed period, not null

Throw:

*

DateTimeParseException if the text cannot be parsed to a period

Public Methods

public addTo(temporal: Temporal): Temporal source

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.

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

The calculation will add the years, then months, then days. Only non-zero amounts will be added. If the date-time has a calendar system with a fixed number of months in a year, then the years and months will be combined before being added.

This instance is immutable and unaffected by this method call.

Override:

TemporalAmount#addTo

Params:

NameTypeAttributeDescription
temporal Temporal

the temporal object to adjust, not null

Return:

Temporal

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

Throw:

*

DateTimeException if unable to add

*

ArithmeticException if numeric overflow occurs

public chronology(): IsoChronology source

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.

Return:

IsoChronology

the chronology defining the period, not null

public days(): number source

Gets the amount of days of this period.

This returns the days unit.

Return:

number

the amount of days of this period, may be negative

public equals(obj: *): boolean source

Checks if this period is equal to another period.

The comparison is based on the amounts held in the period. 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'.

Params:

NameTypeAttributeDescription
obj *

the object to check, null returns false

Return:

boolean

true if this is equal to the other period

public get(unit: TemporalUnit): number source

Gets the value of the requested unit.

The supported units are chronology specific. They will typically be ChronoUnit#YEARS, ChronoUnit#MONTHS and ChronoUnit#DAYS. Requesting an unsupported unit will throw an exception.

Override:

TemporalAmount#get

Params:

NameTypeAttributeDescription
unit TemporalUnit

the TemporalUnit for which to return the value

Return:

number

the long value of the unit

Throw:

*

DateTimeException if the unit is not supported

*

UnsupportedTemporalTypeException if the unit is not supported

public hashCode(): number source

A hash code for this period.

Return:

number

a suitable hash code

public isNegative(): boolean source

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.

Return:

boolean

true if any unit of this period is negative

public isZero(): boolean source

Checks if all three units of this period are zero.

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

Return:

boolean

true if this period is zero-length

public minus(amountToSubtract: TemporalAmount): Period source

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

This input amount is converted to a Period using from. This operates separately on the years, months and days.

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

This instance is immutable and unaffected by this method call.

Params:

NameTypeAttributeDescription
amountToSubtract TemporalAmount

the period to subtract, not null

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public minusDays(daysToSubtract: number): Period source

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.

Params:

NameTypeAttributeDescription
daysToSubtract number

the months to subtract, positive or negative

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public minusMonths(monthsToSubtract: number): Period source

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.

Params:

NameTypeAttributeDescription
monthsToSubtract number

the years to subtract, positive or negative

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public minusYears(yearsToSubtract: number): Period source

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.

Params:

NameTypeAttributeDescription
yearsToSubtract number

the years to subtract, positive or negative

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public months(): number source

Gets the amount of months of this period.

This returns the months unit.

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

Return:

number

the amount of months of this period, may be negative

public multipliedBy(scalar: number): Period source

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

This simply multiplies each field, years, months, days and normalized time, by the scalar. No normalization is performed.

Params:

NameTypeAttributeDescription
scalar number

the scalar to multiply by, not null

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public negated(): Period source

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

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public normalized(): Period source

Returns a copy of this period with the years and months normalized using a 12 month year.

This normalizes the years and months units, leaving the days unit unchanged. 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'.

This normalization uses a 12 month year which is not valid for all calendar systems.

This instance is immutable and unaffected by this method call.

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public plus(amountToAdd: TemporalAmount): Period source

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

This input amount is converted to a Period using from. This operates separately on the years, months and days.

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

This instance is immutable and unaffected by this method call.

Params:

NameTypeAttributeDescription
amountToAdd TemporalAmount

the period to add, not null

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public plusDays(daysToAdd: number): Period source

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.

Params:

NameTypeAttributeDescription
daysToAdd number

the days to add, positive or negative

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public plusMonths(monthsToAdd: number): Period source

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.

Params:

NameTypeAttributeDescription
monthsToAdd number

the months to add, positive or negative

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public plusYears(yearsToAdd: number): Period source

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.

Params:

NameTypeAttributeDescription
yearsToAdd number

the years to add, positive or negative

Return:

Period

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

Throw:

*

ArithmeticException if numeric overflow occurs

public subtractFrom(temporal: Temporal): Temporal source

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.

  // 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 added if non-zero, otherwise the combination of years and months is added if non-zero. Finally, any days are added.

The calculation will subtract the years, then months, then days. Only non-zero amounts will be subtracted. If the date-time has a calendar system with a fixed number of months in a year, then the years and months will be combined before being subtracted.

This instance is immutable and unaffected by this method call.

Override:

TemporalAmount#subtractFrom

Params:

NameTypeAttributeDescription
temporal Temporal

the temporal object to adjust, not null

Return:

Temporal

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

Throw:

*

DateTimeException if unable to subtract

*

ArithmeticException if numeric overflow occurs

public toJSON(): string source

Return:

string

same as Period.toString

public toString(): string source

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

Return:

string

a string representation of this period, not null

public toTotalMonths(): number source

Gets the total number of months in this period using a 12 month year.

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 uses a 12 month year which is not valid for all calendar systems.

This instance is immutable and unaffected by this method call.

Return:

number

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

public units(): ChronoUnit[] source

Gets the list of units, from largest to smallest, that fully define this amount.

Override:

TemporalAmount#units

Return:

ChronoUnit[]

list of units

public withDays(days: number): Period source

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.

Params:

NameTypeAttributeDescription
days number

the days to represent, may be negative

Return:

Period

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

public withMonths(months: number): Period source

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

Params:

NameTypeAttributeDescription
months number

the months to represent, may be negative

Return:

Period

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

public withYears(years: number): Period source

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

Params:

NameTypeAttributeDescription
years number

the years to represent, may be negative

Return:

Period

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

public years(): number source

Gets the amount of years of this period.

This returns the years unit.

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

Return:

number

the amount of years of this period, may be negative