public final class Period extends java.lang.Object implements TemporalAdder, TemporalSubtractor, java.io.Serializable
A Period
represents an amount of time measured in terms of the most commonly used units:
YEARS
MONTHS
DAYS
The period may be used with any calendar system with the exception is methods with an "ISO" suffix. The meaning of a "year" or a "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.
long
nanoseconds for all time units internally.Modifier and Type | Field and Description |
---|---|
static Period |
ZERO
A constant for a period of zero.
|
Modifier and Type | Method and Description |
---|---|
Temporal |
addTo(Temporal temporal)
Adds this period to the specified temporal object.
|
static Period |
between(TemporalAccessor start,
TemporalAccessor end)
Returns a
Period consisting of the number of years, months, days,
hours, minutes, seconds, and nanoseconds between two TemporalAccessor instances. |
static Period |
betweenISO(LocalDate startDate,
LocalDate endDate)
Obtains a
Period consisting of the number of years, months,
and days between two dates. |
static Period |
betweenISO(LocalTime startTime,
LocalTime endTime)
Obtains a
Period consisting of the number of hours, minutes,
seconds and nanoseconds between two times. |
boolean |
equals(java.lang.Object obj)
Checks if this period is equal to another period.
|
int |
getDays()
Gets the amount of days of this period.
|
int |
getHours()
Gets the amount of hours of this period.
|
int |
getMinutes()
Gets the amount of minutes within an hour of this period.
|
int |
getMonths()
Gets the amount of months of this period.
|
int |
getNanos()
Gets the amount of nanoseconds within a second of this period.
|
int |
getSeconds()
Gets the amount of seconds within a minute of this period.
|
long |
getTimeNanos()
Gets the total amount of the time units of this period, measured in nanoseconds.
|
int |
getYears()
Gets the amount of years of this period.
|
int |
hashCode()
A hash code for this period.
|
boolean |
isPositive()
Checks if this period is fully positive, excluding zero.
|
boolean |
isZero()
Checks if this period is zero-length.
|
Period |
minus(long amount,
TemporalUnit unit)
Returns a copy of this period with the specified period subtracted.
|
Period |
minus(Period other)
Returns a copy of this period with the specified period subtracted.
|
Period |
minusDays(long amount) |
Period |
minusHours(long amount) |
Period |
minusMinutes(long amount) |
Period |
minusMonths(long amount) |
Period |
minusNanos(long amount) |
Period |
minusSeconds(long amount) |
Period |
minusYears(long amount) |
Period |
multipliedBy(int scalar)
Returns a new instance with each element in this period multiplied
by the specified scalar.
|
Period |
negated()
Returns a new instance with each amount in this period negated.
|
Period |
normalizedDaysToHours()
Returns a copy of this period with any days converted to hours using a 24 hour day.
|
Period |
normalizedHoursToDays()
Returns a copy of this period with the days and hours normalized using a 24 hour day.
|
Period |
normalizedMonthsISO()
Returns a copy of this period with the years and months normalized using a 12 month year.
|
static Period |
of(Duration duration)
Obtains a
Period from a Duration . |
static Period |
of(int years,
int months,
int days,
int hours,
int minutes,
int seconds)
Obtains a
Period from date-based and time-based fields. |
static Period |
of(int years,
int months,
int days,
int hours,
int minutes,
int seconds,
long nanos)
Obtains a
Period from date-based and time-based fields. |
static Period |
of(long amount,
TemporalUnit unit)
Obtains an instance of
Period from a period in the specified unit. |
static Period |
ofDate(int years,
int months,
int days)
Obtains a
Period from date-based fields. |
static Period |
ofTime(int hours,
int minutes,
int seconds)
Obtains a
Period from time-based fields. |
static Period |
ofTime(int hours,
int minutes,
int seconds,
long nanos)
Obtains a
Period from time-based fields. |
static Period |
parse(java.lang.CharSequence text)
Obtains a
Period from a text string such as PnYnMnDTnHnMn.nS . |
Period |
plus(long amount,
TemporalUnit unit)
Returns a copy of this period with the specified period added.
|
Period |
plus(Period other)
Returns a copy of this period with the specified period added.
|
Period |
plusDays(long amount) |
Period |
plusHours(long amount) |
Period |
plusMinutes(long amount) |
Period |
plusMonths(long amount) |
Period |
plusNanos(long amount) |
Period |
plusSeconds(long amount) |
Period |
plusYears(long amount) |
Temporal |
subtractFrom(Temporal temporal)
Subtracts this period from the specified temporal object.
|
Period |
toDateOnly()
Converts this period to one that only has date units.
|
Duration |
toDuration()
Calculates the duration of this period.
|
java.lang.String |
toString()
Outputs this period as a
String , such as P6Y3M1DT12H . |
Period |
toTimeOnly()
Converts this period to one that only has time units.
|
Period |
withDays(int days)
Returns a copy of this period with the specified amount of days.
|
Period |
withMonths(int months)
Returns a copy of this period with the specified amount of months.
|
Period |
withTimeNanos(long nanos)
Returns a copy of this period with the specified total amount of time units
expressed in nanoseconds.
|
Period |
withYears(int years)
Returns a copy of this period with the specified amount of years.
|
public static final Period ZERO
public static Period of(int years, int months, int days, int hours, int minutes, int seconds)
Period
from date-based and time-based fields.
This creates an instance based on years, months, days, hours, minutes and seconds. Within a period, the time fields are always normalized.
years
- the amount of years, may be negativemonths
- the amount of months, may be negativedays
- the amount of days, may be negativehours
- the amount of hours, may be negativeminutes
- the amount of minutes, may be negativeseconds
- the amount of seconds, may be negativepublic static Period of(int years, int months, int days, int hours, int minutes, int seconds, long nanos)
Period
from date-based and time-based fields.
This creates an instance based on years, months, days, hours, minutes, seconds and nanoseconds. Within a period, the time fields are always normalized.
years
- the amount of years, may be negativemonths
- the amount of months, may be negativedays
- the amount of days, may be negativehours
- the amount of hours, may be negativeminutes
- the amount of minutes, may be negativeseconds
- the amount of seconds, may be negativenanos
- the amount of nanos, may be negativepublic static Period ofDate(int years, int months, int days)
Period
from date-based fields.
This creates an instance based on years, months and days.
years
- the amount of years, may be negativemonths
- the amount of months, may be negativedays
- the amount of days, may be negativepublic static Period ofTime(int hours, int minutes, int seconds)
Period
from time-based fields.
This creates an instance based on hours, minutes and seconds. Within a period, the time fields are always normalized.
hours
- the amount of hours, may be negativeminutes
- the amount of minutes, may be negativeseconds
- the amount of seconds, may be negativepublic static Period ofTime(int hours, int minutes, int seconds, long nanos)
Period
from time-based fields.
This creates an instance based on hours, minutes, seconds and nanoseconds. Within a period, the time fields are always normalized.
hours
- the amount of hours, may be negativeminutes
- the amount of minutes, may be negativeseconds
- the amount of seconds, may be negativenanos
- the amount of nanos, may be negativepublic static Period of(long amount, TemporalUnit unit)
Period
from a period in the specified unit.
The parameters represent the two parts of a phrase like '6 Days'. For example:
Period.of(3, SECONDS); Period.of(5, YEARS);The specified unit must be one of the supported units from
ChronoUnit
,
YEARS
, MONTHS
or DAYS
or be a time unit with an
exact duration.
Other units throw an exception.amount
- the amount of the period, measured in terms of the unit, positive or negativeunit
- the unit that the period is measured in, must have an exact duration, not nullDateTimeException
- if the period unit is invalidjava.lang.ArithmeticException
- if a numeric overflow occurspublic static Period of(Duration duration)
Period
from a Duration
.
This converts the duration to a period. Within a period, the time fields are always normalized. The years, months and days fields will be zero.
To populate the days field, call normalizedHoursToDays()
on the created period.
duration
- the duration to convert, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic static Period between(TemporalAccessor start, TemporalAccessor end)
Period
consisting of the number of years, months, days,
hours, minutes, seconds, and nanoseconds between two TemporalAccessor
instances.
The start date is included, but the end date is not. Only whole years count.
For example, from 2010-01-15
to 2011-03-18
is one year, two months and three days.
This method examines the fields
YEAR
, MONTH_OF_YEAR
,
DAY_OF_MONTH
and NANO_OF_DAY
The difference between each of the fields is calculated independently from the others.
At least one of the four fields must be present.
The four units are typically retained without normalization. However, years and months are normalized if the range of months is fixed, as it is with ISO.
The result of this method can be a negative period if the end is before the start. The negative sign can be different in each of the four major units.
start
- the start date, inclusive, not nullend
- the end date, exclusive, not nullDateTimeException
- if the two date-times do have similar available fieldsjava.lang.ArithmeticException
- if numeric overflow occurspublic static Period betweenISO(LocalDate startDate, LocalDate endDate)
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.
startDate
- the start date, inclusive, not nullendDate
- the end date, exclusive, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic static Period betweenISO(LocalTime startTime, LocalTime endTime)
Period
consisting of the number of hours, minutes,
seconds and nanoseconds between two times.
The start time is included, but the end time is not.
The period is calculated from the difference between the nano-of-day values
of the two times. For example, from 13:45:00
to 14:50:30.123456789
is P1H5M30.123456789S
.
The result of this method can be a negative period if the end is before the start.
startTime
- the start time, inclusive, not nullendTime
- the end time, exclusive, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic static Period parse(java.lang.CharSequence text)
Period
from a text string such as PnYnMnDTnHnMn.nS
.
This will parse the string produced by toString()
which is
a subset of the ISO-8601 period format PnYnMnDTnHnMn.nS
.
The string consists of a series of numbers with a suffix identifying their meaning.
The values, and suffixes, must be in the sequence year, month, day, hour, minute, second.
Any of the number/suffix pairs may be omitted providing at least one is present.
If the period is zero, the value is normally represented as PT0S
.
The numbers must consist of ASCII digits.
Any of the numbers may be negative. Negative zero is not accepted.
The number of nanoseconds is expressed as an optional fraction of the seconds.
There must be at least one digit before any decimal point.
There must be between 1 and 9 inclusive digits after any decimal point.
The letters will all be accepted in upper or lower case.
The decimal point may be either a dot or a comma.
text
- the text to parse, not nullDateTimeParseException
- if the text cannot be parsed to a periodpublic boolean isZero()
public boolean isPositive()
This checks whether all the amounts in the period are positive, defined as greater than zero.
public int getYears()
public int getMonths()
public int getDays()
public int getHours()
Within a period, the time fields are always normalized.
public int getMinutes()
Within a period, the time fields are always normalized.
public int getSeconds()
Within a period, the time fields are always normalized.
public int getNanos()
Within a period, the time fields are always normalized.
public long getTimeNanos()
Within a period, the time fields are always normalized.
public Period withYears(int years)
This method will only affect the years field. All other units are unaffected.
This instance is immutable and unaffected by this method call.
years
- the years to representPeriod
based on this period with the requested years, not nullpublic Period withMonths(int months)
This method will only affect the months field. All other units are unaffected.
This instance is immutable and unaffected by this method call.
months
- the months to representPeriod
based on this period with the requested months, not nullpublic Period withDays(int days)
This method will only affect the days field. All other units are unaffected.
This instance is immutable and unaffected by this method call.
days
- the days to representPeriod
based on this period with the requested days, not nullpublic Period withTimeNanos(long nanos)
Within a period, the time fields are always normalized. This method will affect all the time units - hours, minutes, seconds and nanos. The date units are unaffected.
This instance is immutable and unaffected by this method call.
nanos
- the nanoseconds to representPeriod
based on this period with the requested nanoseconds, not nullpublic Period plus(Period other)
This operates separately on the years, months, days and the normalized time. There is no further normalization beyond the normalized time.
This instance is immutable and unaffected by this method call.
other
- the period to add, not nullPeriod
based on this period with the requested period added, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period plus(long amount, TemporalUnit unit)
The specified unit must be one of the supported units from ChronoUnit
,
YEARS
, MONTHS
or DAYS
or be a time unit with an
exact duration.
Other units throw an exception.
This instance is immutable and unaffected by this method call.
amount
- the amount to add, positive or negativeunit
- the unit that the amount is expressed in, not nullPeriod
based on this period with the requested amount added, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period plusYears(long amount)
public Period plusMonths(long amount)
public Period plusDays(long amount)
public Period plusHours(long amount)
public Period plusMinutes(long amount)
public Period plusSeconds(long amount)
public Period plusNanos(long amount)
public Period minus(Period other)
This operates separately on the years, months, days and the normalized time. There is no further normalization beyond the normalized time.
This instance is immutable and unaffected by this method call.
other
- the period to subtract, not nullPeriod
based on this period with the requested period subtracted, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period minus(long amount, TemporalUnit unit)
The specified unit must be one of the supported units from ChronoUnit
,
YEARS
, MONTHS
or DAYS
or be a time unit with an
exact duration.
Other units throw an exception.
This instance is immutable and unaffected by this method call.
amount
- the amount to subtract, positive or negativeunit
- the unit that the amount is expressed in, not nullPeriod
based on this period with the requested amount subtracted, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period minusYears(long amount)
public Period minusMonths(long amount)
public Period minusDays(long amount)
public Period minusHours(long amount)
public Period minusMinutes(long amount)
public Period minusSeconds(long amount)
public Period minusNanos(long amount)
public Period multipliedBy(int 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 nullPeriod
based on this period with the amounts multiplied by the scalar, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period negated()
Period
based on this period with the amounts negated, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period normalizedHoursToDays()
This normalizes the days and hours units, leaving years and months unchanged.
The hours unit is adjusted to have an absolute value less than 23,
with the days unit being adjusted to compensate.
For example, a period of P1DT27H
will be normalized to P2DT3H
.
The sign of the days and hours units will be the same after normalization.
For example, a period of P1DT-51H
will be normalized to P-1DT-3H
.
Since all time units are always normalized, if the hours units changes sign then
other time units will also be affected.
This instance is immutable and unaffected by this method call.
Period
based on this period with excess hours normalized to days, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period normalizedDaysToHours()
The days unit is reduced to zero, with the hours unit increased by 24 times the
days unit to compensate. Other units are unaffected.
For example, a period of P2DT4H
will be normalized to PT52H
.
This instance is immutable and unaffected by this method call.
Period
based on this period with days normalized to hours, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period normalizedMonthsISO()
This normalizes the years and months units, leaving other units 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 P1Y15M
will be normalized to P2Y3M
.
The sign of the years and months units will be the same after normalization.
For example, a period of P1Y-25M
will be normalized to P-1Y-1M
.
This normalization uses a 12 month year it is not valid for all calendar systems.
This instance is immutable and unaffected by this method call.
Period
based on this period with years and months normalized, not nulljava.lang.ArithmeticException
- if numeric overflow occurspublic Period toDateOnly()
The resulting period will have the same years, months and days as this period
but the time units will all be zero. No normalization occurs in the calculation.
For example, a period of P1Y3MT12H
will be converted to P1Y3M
.
This instance is immutable and unaffected by this method call.
Period
based on this period with the time units set to zero, not nullpublic Temporal addTo(Temporal temporal)
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(TemporalAdder)
.
// 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, then nanos. 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.
addTo
in interface TemporalAdder
temporal
- the temporal object to adjust, not nullDateTimeException
- if unable to addjava.lang.ArithmeticException
- if numeric overflow occurspublic Temporal subtractFrom(Temporal temporal)
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(TemporalSubtractor)
.
// these two lines are equivalent, but the second approach is recommended dateTime = thisPeriod.subtractFrom(dateTime); dateTime = dateTime.minus(thisPeriod);
The calculation will subtract the years, then months, then days, then nanos. 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.
subtractFrom
in interface TemporalSubtractor
temporal
- the temporal object to adjust, not nullDateTimeException
- if unable to subtractjava.lang.ArithmeticException
- if numeric overflow occurspublic Period toTimeOnly()
The resulting period will have the same time units as this period
but the date units will all be zero. No normalization occurs in the calculation.
For example, a period of P1Y3MT12H
will be converted to PT12H
.
This instance is immutable and unaffected by this method call.
Period
based on this period with the date units set to zero, not nullpublic Duration toDuration()
The calculation uses the hours, minutes, seconds and nanoseconds fields.
If years, months or days are present an exception is thrown.
See toTimeOnly()
for a way to remove the date units and
normalizedDaysToHours()
for a way to convert days to hours.
Duration
equivalent to this period, not nullDateTimeException
- if the period cannot be converted as it contains years, months or dayspublic boolean equals(java.lang.Object obj)
The comparison is based on the amounts held in the period. To be equal, the years, months, days and normalized time fields must be equal.
equals
in class java.lang.Object
obj
- the object to check, null returns falseObject.hashCode()
,
HashMap
public int hashCode()
hashCode
in class java.lang.Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public java.lang.String toString()
String
, such as P6Y3M1DT12H
.
The output will be in the ISO-8601 period format.
toString
in class java.lang.Object