Class Interval
- All Implemented Interfaces:
 Serializable, ChronoPeriod, TemporalAmount
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.
- Since:
 - 0.8.7
 - See Also:
 
- 
Field Summary
Fields - 
Method Summary
Modifier and TypeMethodDescriptionstatic Intervalbooleanstatic Intervalfrom(TemporalAmount amount) Create a newIntervalfrom atemporal amount.longget(TemporalUnit unit) intgetDays()Return the days represented by this interval.Return the hours, minutes, seconds and microseconds asDuration.intgetHours()Return the hours represented by this interval.intReturn the microseconds represented by this interval.intReturn the hours represented by this interval.intReturn the months represented by this interval.Return the years, months, and days asPeriod.doubleReturn the seconds represented by this interval.intReturn the seconds represented by this interval.getUnits()getValue()Return a string representing the interval value.intgetYears()Return the years represented by this interval.inthashCode()minus(TemporalAmount amountToSubtract) multipliedBy(int scalar) static Intervalof(int years, int months, int days, int hours, int minutes, double seconds) static Intervalof(int years, int months, int days, int hours, int minutes, int seconds, int microseconds) static Intervalstatic Intervalstatic Intervalstatic IntervalParse thevaluerepresenting aInterval.plus(TemporalAmount amountToAdd) subtractFrom(Temporal temporal) toString()withDays(int days) Return a new instance ofIntervalapplying the current interval and adjusting years givendays.withHours(int hours) Return a new instance ofIntervalapplying the current interval and adjusting years givenhours.withMicroseconds(int microseconds) Return a new instance ofIntervalapplying the current interval and adjusting years givenmicroseconds.withMinutes(int minutes) Return a new instance ofIntervalapplying the current interval and adjusting years givenminutes.withMonths(int months) Return a copy a new instance ofIntervalapplying the current interval and adjusting years givenmonths.withSeconds(int seconds) Return a new instance ofIntervalapplying the current interval and adjusting years givenseconds.withYears(int years) Return a copy instance ofIntervalapplying the current interval and adjusting years givenyears.Methods inherited from interface ChronoPeriod
isNegative, isZero, negated 
- 
Field Details
- 
ZERO
Constant for a duration of zero. 
 - 
 - 
Method Details
- 
of
 - 
of
 - 
of
 - 
of
- Parameters:
 years- interval yearsmonths- interval monthsdays- interval dayshours- interval hoursminutes- interval minutesseconds- interval seconds including microseconds as fraction- Returns:
 - the new 
Intervalobject 
 - 
of
public static Interval of(int years, int months, int days, int hours, int minutes, int seconds, int microseconds) - Parameters:
 years- interval yearsmonths- interval monthsdays- interval dayshours- interval hoursminutes- interval minutesseconds- interval secondsmicroseconds- interval microseconds- Returns:
 - the new 
Intervalobject 
 - 
between
Create a newIntervalconsisting of the amount of time between twoTemporals.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
LocalDateandLocalTime. 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.- Parameters:
 startInclusive- the start, inclusiveendExclusive- the end, exclusive- Returns:
 - the number of days between this date and the end date, not 
null - Throws:
 IllegalArgumentException- ifstartInclusiveorendExclusiveisnull
 - 
from
Create a newIntervalfrom atemporal amount.This obtains an instance based on the specified amount. A
TemporalAmountrepresents an amount of time which this factory extracts to aInterval.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.- Parameters:
 amount- the temporal amount to convert- Returns:
 - the equivalent duration
 - Throws:
 DateTimeException- if unable to convert to aDurationArithmeticException- if numeric overflow occursIllegalArgumentException- ifamountisnull
 - 
parse
Parse thevaluerepresenting aInterval. This method only supports values that Postgres returns.- Parameters:
 value- a string that represents the interval value- Returns:
 - the new 
Intervalobject - Throws:
 IllegalArgumentException- ifvalueisnullor contains an invalid format
 - 
getYears
public int getYears()Return the years represented by this interval.- Returns:
 - the years represented by this interval
 
 - 
getMonths
public int getMonths()Return the months represented by this interval.- Returns:
 - the months represented by this interval
 
 - 
getDays
public int getDays()Return the days represented by this interval.- Returns:
 - the days represented by this interval
 
 - 
getHours
public int getHours()Return the hours represented by this interval.- Returns:
 - the hours represented by this interval
 
 - 
getMinutes
public int getMinutes()Return the hours represented by this interval.- Returns:
 - the hours represented by this interval
 
 - 
getSeconds
public double getSeconds()Return the seconds represented by this interval.- Returns:
 - seconds represented by this interval
 
 - 
getSecondsInMinute
public int getSecondsInMinute()Return the seconds represented by this interval.- Returns:
 - seconds represented by this interval
 
 - 
getMicrosecondsInSecond
public int getMicrosecondsInSecond()Return the microseconds represented by this interval.- Returns:
 - microseconds represented by this interval
 
 - 
get
- Specified by:
 getin interfaceChronoPeriod- Specified by:
 getin interfaceTemporalAmount
 - 
getUnits
- Specified by:
 getUnitsin interfaceChronoPeriod- Specified by:
 getUnitsin interfaceTemporalAmount
 - 
addTo
- Specified by:
 addToin interfaceChronoPeriod- Specified by:
 addToin interfaceTemporalAmount
 - 
subtractFrom
- Specified by:
 subtractFromin interfaceChronoPeriod- Specified by:
 subtractFromin interfaceTemporalAmount
 - 
plus
- Specified by:
 plusin interfaceChronoPeriod
 - 
minus
- Specified by:
 minusin interfaceChronoPeriod
 - 
getChronology
- Specified by:
 getChronologyin interfaceChronoPeriod
 - 
multipliedBy
- Specified by:
 multipliedByin interfaceChronoPeriod
 - 
normalized
- Specified by:
 normalizedin interfaceChronoPeriod
 - 
withYears
 - 
withMonths
 - 
withDays
 - 
withHours
 - 
withMinutes
 - 
withSeconds
 - 
withMicroseconds
 - 
getPeriod
 - 
getDuration
 - 
getValue
Return a string representing the interval value.- Returns:
 - a string representing the interval value
 
 - 
equals
- Specified by:
 equalsin interfaceChronoPeriod- Overrides:
 equalsin classObject
 - 
hashCode
public int hashCode()- Specified by:
 hashCodein interfaceChronoPeriod- Overrides:
 hashCodein classObject
 - 
toString
- Specified by:
 toStringin interfaceChronoPeriod- Overrides:
 toStringin classObject
 
 -