final class Period extends ChronoPeriod with Serializable
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 YEARS
,
MONTHS
and 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. The normalization assumes a 12 month year, so is not appropriate for all calendar systems.
Specification for implementors
This class is immutable and thread-safe.
- Annotations
- @SerialVersionUID()
- Alphabetic
- By Inheritance
- Period
- Serializable
- ChronoPeriod
- TemporalAmount
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
addTo(temporal: Temporal): Temporal
Adds this period to the specified temporal object.
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 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.
- temporal
the temporal object to adjust, not null
- returns
an object of the same type with the adjustment made, not null
- Definition Classes
- Period → ChronoPeriod → TemporalAmount
- Exceptions thrown
ArithmeticException
if numeric overflow occursDateTimeException
if unable to add
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(obj: Any): Boolean
Checks if this period is equal to another period.
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".
- obj
the object to check, null returns false
- returns
true if this is equal to the other period
- Definition Classes
- Period → ChronoPeriod → AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
get(unit: TemporalUnit): Long
Gets the value of the requested unit.
Gets the value of the requested unit.
The supported units are chronology specific. They will typically be
YEARS
,MONTHS
andDAYS
. Requesting an unsupported unit will throw an exception.- unit
the { @code TemporalUnit} for which to return the value
- returns
the long value of the unit
- Definition Classes
- Period → ChronoPeriod → TemporalAmount
- Exceptions thrown
DateTimeException
if the unit is not supportedUnsupportedTemporalTypeException
if the unit is not supported
-
def
getChronology: Chronology
Gets the chronology that defines the meaning of the supported units.
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
the chronology defining the period, not null
- Definition Classes
- Period → ChronoPeriod
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
getDays: Int
Gets the amount of days of this period.
Gets the amount of days of this period.
This returns the days unit.
- returns
the amount of days of this period, may be negative
-
def
getMonths: Int
Gets the amount of months of this period.
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".
- returns
the amount of months of this period, may be negative
-
def
getUnits: List[TemporalUnit]
Gets the set of units supported by this period.
Gets the set of units supported by this period.
The supported units are chronology specific. They will typically be
YEARS
,MONTHS
andDAYS
. 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
a list containing the supported units, not null
- Definition Classes
- Period → ChronoPeriod → TemporalAmount
-
def
getYears: Int
Gets the amount of years of this period.
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".
- returns
the amount of years of this period, may be negative
-
def
hashCode: Int
A hash code for this period.
A hash code for this period.
- returns
a suitable hash code
- Definition Classes
- Period → ChronoPeriod → AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
isNegative: Boolean
Checks if any of the three units of this period are negative.
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
true if any unit of this period is negative
- Definition Classes
- Period → ChronoPeriod
-
def
isZero: Boolean
Checks if all three units of this period are zero.
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
true if this period is zero-length
- Definition Classes
- Period → ChronoPeriod
-
def
minus(amountToSubtract: TemporalAmount): Period
Returns a copy of this period with the specified amount subtracted.
Returns a copy of this period with the specified amount subtracted.
This input amount is converted to a
Period
usingfrom(TemporalAmount)
. 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.
- amountToSubtract
the period to subtract, not null
- returns
a { @code Period} based on this period with the requested period subtracted, not null
- Definition Classes
- Period → ChronoPeriod
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
minusDays(daysToSubtract: Long): Period
Returns a copy of this period with the specified days subtracted.
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.
- daysToSubtract
the months to subtract, positive or negative
- returns
a { @code Period} based on this period with the specified days subtracted, not null
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
minusMonths(monthsToSubtract: Long): Period
Returns a copy of this period with the specified months subtracted.
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.
- monthsToSubtract
the years to subtract, positive or negative
- returns
a { @code Period} based on this period with the specified months subtracted, not null
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
minusYears(yearsToSubtract: Long): Period
Returns a copy of this period with the specified years subtracted.
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.
- yearsToSubtract
the years to subtract, positive or negative
- returns
a { @code Period} based on this period with the specified years subtracted, not null
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
multipliedBy(scalar: Int): Period
Returns a new instance with each element in this period multiplied by the specified scalar.
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.
- scalar
the scalar to multiply by, not null
- returns
a { @code Period} based on this period with the amounts multiplied by the scalar, not null
- Definition Classes
- Period → ChronoPeriod
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
negated: Period
Returns a new instance with each amount in this period negated.
Returns a new instance with each amount in this period negated.
- returns
a { @code Period} based on this period with the amounts negated, not null
- Definition Classes
- Period → ChronoPeriod
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
normalized: Period
Returns a copy of this period with the years and months normalized using a 12 month year.
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.
- returns
a { @code Period} based on this period with excess months normalized to years, not null
- Definition Classes
- Period → ChronoPeriod
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
plus(amountToAdd: TemporalAmount): Period
Returns a copy of this period with the specified amount added.
Returns a copy of this period with the specified amount added.
This input amount is converted to a
Period
usingfrom(TemporalAmount)
. 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.
- amountToAdd
the period to add, not null
- returns
a { @code Period} based on this period with the requested period added, not null
- Definition Classes
- Period → ChronoPeriod
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
plusDays(daysToAdd: Long): Period
Returns a copy of this period with the specified days added.
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.
- daysToAdd
the days to add, positive or negative
- returns
a { @code Period} based on this period with the specified days added, not null
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
plusMonths(monthsToAdd: Long): Period
Returns a copy of this period with the specified months added.
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.
- monthsToAdd
the months to add, positive or negative
- returns
a { @code Period} based on this period with the specified months added, not null
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
plusYears(yearsToAdd: Long): Period
Returns a copy of this period with the specified years added.
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.
- yearsToAdd
the years to add, positive or negative
- returns
a { @code Period} based on this period with the specified years added, not null
- Exceptions thrown
ArithmeticException
if numeric overflow occurs
-
def
subtractFrom(temporal: Temporal): Temporal
Subtracts this period from the specified temporal object.
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 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.
- temporal
the temporal object to adjust, not null
- returns
an object of the same type with the adjustment made, not null
- Definition Classes
- Period → ChronoPeriod → TemporalAmount
- Exceptions thrown
ArithmeticException
if numeric overflow occursDateTimeException
if unable to subtract
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString: String
Outputs this period as a
String
, such asP6Y3M1D
.Outputs this period as a
String
, such asP6Y3M1D
.The output will be in the ISO-8601 period format. A zero period will be represented as zero days, 'P0D'.
- returns
a string representation of this period, not null
- Definition Classes
- Period → ChronoPeriod → AnyRef → Any
-
def
toTotalMonths: Long
Gets the total number of months in this period using a 12 month year.
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.
- returns
the total number of months in the period, may be negative
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
withDays(days: Int): Period
Returns a copy of this period with the specified amount of 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.
- days
the days to represent, may be negative
- returns
a { @code Period} based on this period with the requested days, not null
-
def
withMonths(months: Int): Period
Returns a copy of this period with the specified amount of 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 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.
- months
the months to represent, may be negative
- returns
a { @code Period} based on this period with the requested months, not null
-
def
withYears(years: Int): Period
Returns a copy of this period with the specified amount of 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 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.
- years
the years to represent, may be negative
- returns
a { @code Period} based on this period with the requested years, not null