public final class Interval extends BaseInterval implements ReadableInterval, Serializable
A time interval represents a period of time between two instants. Intervals are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.
Intervals have a fixed millisecond duration.
This is the difference between the start and end instants.
The duration is represented separately by ReadableDuration
.
As a result, intervals are not comparable.
To compare the length of two intervals, you should compare their durations.
An interval can also be converted to a ReadablePeriod
.
This represents the difference between the start and end points in terms of fields
such as years and days.
Interval is thread-safe and immutable.
Constructor and Description |
---|
Interval(long startInstant,
long endInstant)
Constructs an interval from a start and end instant with the ISO
default chronology in the default time zone.
|
Interval(long startInstant,
long endInstant,
Chronology chronology)
Constructs an interval from a start and end instant with the
specified chronology.
|
Interval(long startInstant,
long endInstant,
DateTimeZone zone)
Constructs an interval from a start and end instant with the ISO
default chronology in the specified time zone.
|
Interval(Object interval)
Constructs a time interval by converting or copying from another object.
|
Interval(Object interval,
Chronology chronology)
Constructs a time interval by converting or copying from another object,
overriding the chronology.
|
Interval(ReadableDuration duration,
ReadableInstant end)
Constructs an interval from a millisecond duration and an end instant.
|
Interval(ReadableInstant start,
ReadableDuration duration)
Constructs an interval from a start instant and a duration.
|
Interval(ReadableInstant start,
ReadableInstant end)
Constructs an interval from a start and end instant.
|
Interval(ReadableInstant start,
ReadablePeriod period)
Constructs an interval from a start instant and a time period.
|
Interval(ReadablePeriod period,
ReadableInstant end)
Constructs an interval from a time period and an end instant.
|
Modifier and Type | Method and Description |
---|---|
boolean |
abuts(ReadableInterval interval)
Does this interval abut with the interval specified.
|
Interval |
gap(ReadableInterval interval)
Gets the gap between this interval and another interval.
|
Interval |
overlap(ReadableInterval interval)
Gets the overlap between this interval and another interval.
|
static Interval |
parse(String str)
Parses an
Interval from the specified string. |
static Interval |
parseWithOffset(String str)
Parses an
Interval from the specified string, using any offset it contains. |
Interval |
toInterval()
Get this interval as an immutable
Interval object
by returning this . |
Interval |
withChronology(Chronology chronology)
Creates a new interval with the same start and end, but a different chronology.
|
Interval |
withDurationAfterStart(ReadableDuration duration)
Creates a new interval with the specified duration after the start instant.
|
Interval |
withDurationBeforeEnd(ReadableDuration duration)
Creates a new interval with the specified duration before the end instant.
|
Interval |
withEnd(ReadableInstant end)
Creates a new interval with the specified end instant.
|
Interval |
withEndMillis(long endInstant)
Creates a new interval with the specified end millisecond instant.
|
Interval |
withPeriodAfterStart(ReadablePeriod period)
Creates a new interval with the specified period after the start instant.
|
Interval |
withPeriodBeforeEnd(ReadablePeriod period)
Creates a new interval with the specified period before the end instant.
|
Interval |
withStart(ReadableInstant start)
Creates a new interval with the specified start instant.
|
Interval |
withStartMillis(long startInstant)
Creates a new interval with the specified start millisecond instant.
|
getChronology, getEndMillis, getStartMillis, setInterval
checkInterval, contains, contains, contains, containsNow, equals, getEnd, getStart, hashCode, isAfter, isAfter, isAfter, isAfterNow, isBefore, isBefore, isBefore, isBeforeNow, isEqual, overlaps, toDuration, toDurationMillis, toMutableInterval, toPeriod, toPeriod, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
contains, contains, equals, getChronology, getEnd, getEndMillis, getStart, getStartMillis, hashCode, isAfter, isAfter, isBefore, isBefore, overlaps, toDuration, toDurationMillis, toMutableInterval, toPeriod, toPeriod, toString
public Interval(long startInstant, long endInstant)
startInstant
- start of this interval, as milliseconds from 1970-01-01T00:00:00Z.endInstant
- end of this interval, as milliseconds from 1970-01-01T00:00:00Z.IllegalArgumentException
- if the end is before the startpublic Interval(long startInstant, long endInstant, DateTimeZone zone)
startInstant
- start of this interval, as milliseconds from 1970-01-01T00:00:00Z.endInstant
- end of this interval, as milliseconds from 1970-01-01T00:00:00Z.zone
- the time zone to use, null means default zoneIllegalArgumentException
- if the end is before the startpublic Interval(long startInstant, long endInstant, Chronology chronology)
chronology
- the chronology to use, null is ISO defaultstartInstant
- start of this interval, as milliseconds from 1970-01-01T00:00:00Z.endInstant
- end of this interval, as milliseconds from 1970-01-01T00:00:00Z.IllegalArgumentException
- if the end is before the startpublic Interval(ReadableInstant start, ReadableInstant end)
The chronology used is that of the start instant.
start
- start of this interval, null means nowend
- end of this interval, null means nowIllegalArgumentException
- if the end is before the startpublic Interval(ReadableInstant start, ReadableDuration duration)
start
- start of this interval, null means nowduration
- the duration of this interval, null means zero lengthIllegalArgumentException
- if the end is before the startArithmeticException
- if the end instant exceeds the capacity of a longpublic Interval(ReadableDuration duration, ReadableInstant end)
duration
- the duration of this interval, null means zero lengthend
- end of this interval, null means nowIllegalArgumentException
- if the end is before the startArithmeticException
- if the start instant exceeds the capacity of a longpublic Interval(ReadableInstant start, ReadablePeriod period)
When forming the interval, the chronology from the instant is used if present, otherwise the chronology of the period is used.
start
- start of this interval, null means nowperiod
- the period of this interval, null means zero lengthIllegalArgumentException
- if the end is before the startArithmeticException
- if the end instant exceeds the capacity of a longpublic Interval(ReadablePeriod period, ReadableInstant end)
When forming the interval, the chronology from the instant is used if present, otherwise the chronology of the period is used.
period
- the period of this interval, null means zero lengthend
- end of this interval, null means nowIllegalArgumentException
- if the end is before the startArithmeticException
- if the start instant exceeds the capacity of a longpublic Interval(Object interval)
The recognised object types are defined in
ConverterManager
and
include ReadableInterval and String.
The String formats are described by ISODateTimeFormat.dateTimeParser()
and ISOPeriodFormat.standard()
, and may be 'datetime/datetime',
'datetime/period' or 'period/datetime'.
interval
- the time interval to copyIllegalArgumentException
- if the interval is invalidpublic Interval(Object interval, Chronology chronology)
The recognised object types are defined in
ConverterManager
and
include ReadableInterval and String.
The String formats are described by ISODateTimeFormat.dateTimeParser()
and ISOPeriodFormat.standard()
, and may be 'datetime/datetime',
'datetime/period' or 'period/datetime'.
interval
- the time interval to copychronology
- the chronology to use, null means ISO defaultIllegalArgumentException
- if the interval is invalidpublic static Interval parse(String str)
Interval
from the specified string.
The String formats are described by ISODateTimeFormat.dateTimeParser()
and ISOPeriodFormat.standard()
, and may be 'datetime/datetime',
'datetime/period' or 'period/datetime'.
This method operates by parsing in the default time-zone.
Any offset contained within the string being parsed will be normalised to the
offset of the default time-zone. See also parseWithOffset(String)
.
str
- the string to parse, not nullpublic static Interval parseWithOffset(String str)
Interval
from the specified string, using any offset it contains.
The String formats are described by
ISODateTimeFormat.dateTimeParser()
.withOffsetParsed()
and ISOPeriodFormat.standard()
, and may be 'datetime/datetime',
'datetime/period' or 'period/datetime'.
Sometimes this method and new Interval(str)
return different results.
This can be confusing as the difference is not visible in AbstractInterval.toString()
.
When passed a string without an offset, such as '2010-06-30T01:20/P1D',
both the constructor and this method use the default time-zone.
As such, Interval.parseWithOffset("2010-06-30T01:20/P1D")
and
new Interval("2010-06-30T01:20/P1D"))
are equal.
However, when this method is passed a string with an offset,
the offset is directly parsed and stored.
As such, Interval.parseWithOffset("2010-06-30T01:20+02:00/P1D")
and
new Interval("2010-06-30T01:20+02:00/P1D"))
are NOT equal.
The object produced via this method has a zone of DateTimeZone.forOffsetHours(2)
.
The object produced via the constructor has a zone of DateTimeZone.getDefault()
.
str
- the string to parse, not nullpublic Interval toInterval()
Interval
object
by returning this
.toInterval
in interface ReadableInterval
toInterval
in class AbstractInterval
this
public Interval overlap(ReadableInterval interval)
Intervals are inclusive of the start instant and exclusive of the end. An interval overlaps another if it shares some common part of the datetime continuum. This method returns the amount of the overlap, only if the intervals actually do overlap. If the intervals do not overlap, then null is returned.
When two intervals are compared the result is one of three states: (a) they abut, (b) there is a gap between them, (c) they overlap. The abuts state takes precedence over the other two, thus a zero duration interval at the start of a larger interval abuts and does not overlap.
The chronology of the returned interval is the same as that of this interval (the chronology of the interval parameter is not used). Note that the use of the chronology was only correctly implemented in version 1.3.
interval
- the interval to examine, null means nowpublic Interval gap(ReadableInterval interval)
Intervals are inclusive of the start instant and exclusive of the end. An interval has a gap to another interval if there is a non-zero duration between them. This method returns the amount of the gap only if the intervals do actually have a gap between them. If the intervals overlap or abut, then null is returned.
When two intervals are compared the result is one of three states: (a) they abut, (b) there is a gap between them, (c) they overlap. The abuts state takes precedence over the other two, thus a zero duration interval at the start of a larger interval abuts and does not overlap.
The chronology of the returned interval is the same as that of this interval (the chronology of the interval parameter is not used). Note that the use of the chronology was only correctly implemented in version 1.3.
interval
- the interval to examine, null means nowpublic boolean abuts(ReadableInterval interval)
Intervals are inclusive of the start instant and exclusive of the end. An interval abuts if it starts immediately after, or ends immediately before this interval without overlap. A zero duration interval abuts with itself.
When two intervals are compared the result is one of three states: (a) they abut, (b) there is a gap between them, (c) they overlap. The abuts state takes precedence over the other two, thus a zero duration interval at the start of a larger interval abuts and does not overlap.
For example:
[09:00 to 10:00) abuts [08:00 to 08:30) = false (completely before) [09:00 to 10:00) abuts [08:00 to 09:00) = true [09:00 to 10:00) abuts [08:00 to 09:01) = false (overlaps) [09:00 to 10:00) abuts [09:00 to 09:00) = true [09:00 to 10:00) abuts [09:00 to 09:01) = false (overlaps) [09:00 to 10:00) abuts [10:00 to 10:00) = true [09:00 to 10:00) abuts [10:00 to 10:30) = true [09:00 to 10:00) abuts [10:30 to 11:00) = false (completely after) [14:00 to 14:00) abuts [14:00 to 14:00) = true [14:00 to 14:00) abuts [14:00 to 15:00) = true [14:00 to 14:00) abuts [13:00 to 14:00) = true
interval
- the interval to examine, null means nowpublic Interval withChronology(Chronology chronology)
chronology
- the chronology to use, null means ISO defaultpublic Interval withStartMillis(long startInstant)
startInstant
- the start instant for the new intervalIllegalArgumentException
- if the resulting interval has end before startpublic Interval withStart(ReadableInstant start)
start
- the start instant for the new interval, null means nowIllegalArgumentException
- if the resulting interval has end before startpublic Interval withEndMillis(long endInstant)
endInstant
- the end instant for the new intervalIllegalArgumentException
- if the resulting interval has end before startpublic Interval withEnd(ReadableInstant end)
end
- the end instant for the new interval, null means nowIllegalArgumentException
- if the resulting interval has end before startpublic Interval withDurationAfterStart(ReadableDuration duration)
duration
- the duration to add to the start to get the new end instant, null means zeroIllegalArgumentException
- if the duration is negativepublic Interval withDurationBeforeEnd(ReadableDuration duration)
duration
- the duration to subtract from the end to get the new start instant, null means zeroIllegalArgumentException
- if the duration is negativepublic Interval withPeriodAfterStart(ReadablePeriod period)
period
- the period to add to the start to get the new end instant, null means zeroIllegalArgumentException
- if the period is negativepublic Interval withPeriodBeforeEnd(ReadablePeriod period)
period
- the period to subtract from the end to get the new start instant, null means zeroIllegalArgumentException
- if the period is negativeCopyright © 2002–2024 Joda.org. All rights reserved.