Class/Object

org.threeten.bp

Period

Related Docs: object Period | package bp

Permalink

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()
Linear Supertypes
Serializable, ChronoPeriod, TemporalAmount, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Period
  2. Serializable
  3. ChronoPeriod
  4. TemporalAmount
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def addTo(temporal: Temporal): Temporal

    Permalink

    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
    PeriodChronoPeriodTemporalAmount
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

    DateTimeException if unable to add

  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(obj: Any): Boolean

    Permalink

    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
    PeriodChronoPeriod → AnyRef → Any
  9. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. def get(unit: TemporalUnit): Long

    Permalink

    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 and DAYS. 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
    PeriodChronoPeriodTemporalAmount
    Exceptions thrown

    DateTimeException if the unit is not supported

    UnsupportedTemporalTypeException if the unit is not supported

  11. def getChronology: Chronology

    Permalink

    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
    PeriodChronoPeriod
  12. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  13. def getDays: Int

    Permalink

    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

  14. def getMonths: Int

    Permalink

    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

  15. def getUnits: List[TemporalUnit]

    Permalink

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

    a list containing the supported units, not null

    Definition Classes
    PeriodChronoPeriodTemporalAmount
  16. def getYears: Int

    Permalink

    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

  17. def hashCode(): Int

    Permalink

    A hash code for this period.

    A hash code for this period.

    returns

    a suitable hash code

    Definition Classes
    PeriodChronoPeriod → AnyRef → Any
  18. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  19. def isNegative: Boolean

    Permalink

    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
    PeriodChronoPeriod
  20. def isZero: Boolean

    Permalink

    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
    PeriodChronoPeriod
  21. def minus(amountToSubtract: TemporalAmount): Period

    Permalink

    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 using from(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
    PeriodChronoPeriod
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

  22. def minusDays(daysToSubtract: Long): Period

    Permalink

    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

  23. def minusMonths(monthsToSubtract: Long): Period

    Permalink

    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

  24. def minusYears(yearsToSubtract: Long): Period

    Permalink

    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

  25. def multipliedBy(scalar: Int): Period

    Permalink

    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
    PeriodChronoPeriod
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

  26. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  27. def negated: Period

    Permalink

    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
    PeriodChronoPeriod
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

  28. def normalized: Period

    Permalink

    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
    PeriodChronoPeriod
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

  29. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  30. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  31. def plus(amountToAdd: TemporalAmount): Period

    Permalink

    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 using from(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
    PeriodChronoPeriod
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

  32. def plusDays(daysToAdd: Long): Period

    Permalink

    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

  33. def plusMonths(monthsToAdd: Long): Period

    Permalink

    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

  34. def plusYears(yearsToAdd: Long): Period

    Permalink

    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

  35. def subtractFrom(temporal: Temporal): Temporal

    Permalink

    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
    PeriodChronoPeriodTemporalAmount
    Exceptions thrown

    ArithmeticException if numeric overflow occurs

    DateTimeException if unable to subtract

  36. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  37. def toString(): String

    Permalink

    Outputs this period as a String, such as P6Y3M1D.

    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

    a string representation of this period, not null

    Definition Classes
    PeriodChronoPeriod → AnyRef → Any
  38. def toTotalMonths: Long

    Permalink

    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

  39. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  40. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  41. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  42. def withDays(days: Int): Period

    Permalink

    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

  43. def withMonths(months: Int): Period

    Permalink

    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

  44. def withYears(years: Int): Period

    Permalink

    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

Inherited from Serializable

Inherited from ChronoPeriod

Inherited from TemporalAmount

Inherited from AnyRef

Inherited from Any

Ungrouped