public final class Interval extends Object implements ChronoPeriod, Serializable
interval
datatype in Postgres.
This class models a quantity or amount of time in terms of years, months, days, hours, minutes, seconds, and microseconds.
In addition to accessing its individual properties, its values can be retrieved as Period
and Duration
.
A physical duration could be of infinite length.
For practicality, the duration is stored using integers without normalization.
Normalizing
applies separate normalization following IsoChronology
.
Days/hours/minutes/seconds/microseconds and years/months are normalized independent from each other to avoid month rollover as the amount of days per month varies by per month and leap year. Day
normalization (e.g. more than 28/29/30/31 days) can only be applied in the context of an actual date hence it's not supported by this class.
Normalizing an interval of 14 months and 43 days results in an interval of 1 year, 2 months, and 43 days.
An interval
can be created either from a ISO-8601
or Postgres representation using parse(String)
. It can also be created from a TemporalAmount
or as function
accepting temporal
start
and end
values through between(Temporal, Temporal)
.
This class is immutable and thread-safe.
Duration
,
Period
,
ChronoPeriod
,
Serialized FormModifier and Type | Field and Description |
---|---|
static Interval |
ZERO
Constant for a duration of zero.
|
Modifier and Type | Method and Description |
---|---|
Temporal |
addTo(Temporal temporal) |
static Interval |
between(Temporal startInclusive,
Temporal endExclusive)
|
boolean |
equals(Object o) |
static Interval |
from(TemporalAmount amount)
Create a new
Interval from a temporal amount . |
long |
get(TemporalUnit unit) |
Chronology |
getChronology() |
int |
getDays()
Return the days represented by this interval.
|
Duration |
getDuration()
Return the hours, minutes, seconds and microseconds as
Duration . |
int |
getHours()
Return the hours represented by this interval.
|
int |
getMicrosecondsInSecond()
Return the microseconds represented by this interval.
|
int |
getMinutes()
Return the hours represented by this interval.
|
int |
getMonths()
Return the months represented by this interval.
|
Period |
getPeriod()
Return the years, months, and days as
Period . |
double |
getSeconds()
Return the seconds represented by this interval.
|
int |
getSecondsInMinute()
Return the seconds represented by this interval.
|
List<TemporalUnit> |
getUnits() |
String |
getValue()
Return a string representing the interval value.
|
int |
getYears()
Return the years represented by this interval.
|
int |
hashCode() |
Interval |
minus(TemporalAmount amountToSubtract) |
Interval |
multipliedBy(int scalar) |
Interval |
normalized() |
static Interval |
of(Duration duration)
|
static Interval |
of(int years,
int months,
int days,
int hours,
int minutes,
double seconds)
|
static Interval |
of(int years,
int months,
int days,
int hours,
int minutes,
int seconds,
int microseconds)
|
static Interval |
of(Period period)
|
static Interval |
of(Period period,
Duration duration)
|
static Interval |
parse(String value)
Parse the
value representing a Interval . |
Interval |
plus(TemporalAmount amountToAdd) |
Temporal |
subtractFrom(Temporal temporal) |
String |
toString() |
Interval |
withDays(int days)
Return a new instance of
Interval applying the current interval and adjusting years given days . |
Interval |
withHours(int hours)
Return a new instance of
Interval applying the current interval and adjusting years given hours . |
Interval |
withMicroseconds(int microseconds)
Return a new instance of
Interval applying the current interval and adjusting years given microseconds . |
Interval |
withMinutes(int minutes)
Return a new instance of
Interval applying the current interval and adjusting years given minutes . |
Interval |
withMonths(int months)
Return a copy a new instance of
Interval applying the current interval and adjusting years given months . |
Interval |
withSeconds(int seconds)
Return a new instance of
Interval applying the current interval and adjusting years given seconds . |
Interval |
withYears(int years)
Return a copy instance of
Interval applying the current interval and adjusting years given years . |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
between, isNegative, isZero, negated
public static final Interval ZERO
public static Interval of(Period period)
period
- the period objectInterval
objectIllegalArgumentException
- if period
is null
public static Interval of(Duration duration)
duration
- the duration objectInterval
objectIllegalArgumentException
- if duration
is null
public static Interval of(Period period, Duration duration)
period
- the period objectduration
- the duration objectInterval
objectIllegalArgumentException
- if period
or duration
is null
public static Interval of(int years, int months, int days, int hours, int minutes, double seconds)
years
- interval yearsmonths
- interval monthsdays
- interval dayshours
- interval hoursminutes
- interval minutesseconds
- interval seconds including microseconds as fractionInterval
objectpublic static Interval of(int years, int months, int days, int hours, int minutes, int seconds, int microseconds)
years
- interval yearsmonths
- interval monthsdays
- interval dayshours
- interval hoursminutes
- interval minutesseconds
- interval secondsmicroseconds
- interval microsecondsInterval
objectpublic static Interval between(Temporal startInclusive, Temporal endExclusive)
Interval
consisting of the amount of time between two Temporal
s.
The start is included, but the end is not. The result of this method can be negative if the end is before the start.
The calculation examines the temporals and extracts LocalDate
and LocalTime
.
If the time is missing, it will be defaulted to midnight.
If one date is missing, it will be defaulted to the other date.
It then finds the amount of time between the two dates and between the two times.
startInclusive
- the start, inclusiveendExclusive
- the end, exclusivenull
IllegalArgumentException
- if startInclusive
or endExclusive
is null
public static Interval from(TemporalAmount amount)
Interval
from a temporal amount
.
This obtains an instance based on the specified amount.
A TemporalAmount
represents an amount of time which this factory
extracts to a Interval
.
The result is calculated by looping around each unit in the specified amount.
Any amount that is zero is ignore.
If a unit has an exact duration, it will be totalled using Duration.plus(Duration)
.
If the unit is days or weeks, it will be totalled into the days part of the period.
If the unit is months or quarters, it will be totalled into the months part of the period.
If the unit is years, decades, centuries or millennia, it will be totalled into the years part of the period.
amount
- the temporal amount to convertDateTimeException
- if unable to convert to a Duration
ArithmeticException
- if numeric overflow occursIllegalArgumentException
- if amount
is null
public static Interval parse(String value)
value
representing a Interval
. This method
only supports values that Postgres returns.value
- a string that represents the interval valueInterval
objectIllegalArgumentException
- if value
is null
or contains an invalid formatpublic int getYears()
public int getMonths()
public int getDays()
public int getHours()
public int getMinutes()
public double getSeconds()
public int getSecondsInMinute()
public int getMicrosecondsInSecond()
public long get(TemporalUnit unit)
get
in interface ChronoPeriod
get
in interface TemporalAmount
public List<TemporalUnit> getUnits()
getUnits
in interface ChronoPeriod
getUnits
in interface TemporalAmount
public Temporal addTo(Temporal temporal)
addTo
in interface ChronoPeriod
addTo
in interface TemporalAmount
public Temporal subtractFrom(Temporal temporal)
subtractFrom
in interface ChronoPeriod
subtractFrom
in interface TemporalAmount
public Interval plus(TemporalAmount amountToAdd)
plus
in interface ChronoPeriod
public Interval minus(TemporalAmount amountToSubtract)
minus
in interface ChronoPeriod
public Chronology getChronology()
getChronology
in interface ChronoPeriod
public Interval multipliedBy(int scalar)
multipliedBy
in interface ChronoPeriod
public Interval normalized()
normalized
in interface ChronoPeriod
public Interval withYears(int years)
Interval
applying the current interval and adjusting years given years
.years
- the new years value to applyInterval
with years
appliedpublic Interval withMonths(int months)
Interval
applying the current interval and adjusting years given months
.months
- the new months value to applyInterval
with months
appliedpublic Interval withDays(int days)
Interval
applying the current interval and adjusting years given days
.days
- the new days value to applyInterval
with days
appliedpublic Interval withHours(int hours)
Interval
applying the current interval and adjusting years given hours
.hours
- the new hours value to applyInterval
with hours
appliedpublic Interval withMinutes(int minutes)
Interval
applying the current interval and adjusting years given minutes
.minutes
- the new minutes value to applyInterval
with minutes
appliedpublic Interval withSeconds(int seconds)
Interval
applying the current interval and adjusting years given seconds
.seconds
- the new seconds value to applyInterval
with seconds
appliedpublic Interval withMicroseconds(int microseconds)
Interval
applying the current interval and adjusting years given microseconds
.microseconds
- the new microseconds value to applyInterval
with seconds
appliedpublic Period getPeriod()
Period
.Period
.public Duration getDuration()
Duration
.Duration
.public String getValue()
public boolean equals(Object o)
equals
in interface ChronoPeriod
equals
in class Object
public int hashCode()
hashCode
in interface ChronoPeriod
hashCode
in class Object
public String toString()
toString
in interface ChronoPeriod
toString
in class Object
Copyright © 2021. All rights reserved.