java.io.Serializable
public final class Interval
extends java.lang.Object
implements java.io.Serializable
An interval represents the time on the time-line between two Instant
s.
The class stores the start and end instants, with the start inclusive and the end exclusive.
The end instant is always greater than or equal to the start instant.
The Duration
of an interval can be obtained, but is a separate concept.
An interval is connected to the time-line, whereas a duration is not.
Intervals are not comparable. To compare the length of two intervals, it is generally recommended to compare their durations.
This class must be treated as a value type. Do not synchronize, rely on the identity hash code or use the distinction between equals() and ==.
Modifier and Type | Field | Description |
---|---|---|
static Interval |
ALL |
An interval over the whole time-line.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
abuts(Interval other) |
Checks if this interval abuts the specified interval.
|
boolean |
contains(java.time.Instant instant) |
Checks if this interval contains the specified instant.
|
boolean |
encloses(Interval other) |
Checks if this interval encloses the specified interval.
|
boolean |
equals(java.lang.Object obj) |
Checks if this interval is equal to another interval.
|
java.time.Instant |
getEnd() |
Gets the end of this time interval, exclusive.
|
java.time.Instant |
getStart() |
Gets the start of this time interval, inclusive.
|
int |
hashCode() |
A hash code for this interval.
|
Interval |
intersection(Interval other) |
Calculates the interval that is the intersection of this interval and the specified interval.
|
boolean |
isAfter(java.time.Instant instant) |
Checks if this interval is after the specified instant.
|
boolean |
isAfter(Interval interval) |
Checks if this interval is after the specified interval.
|
boolean |
isBefore(java.time.Instant instant) |
Checks if this interval is before the specified instant.
|
boolean |
isBefore(Interval interval) |
Checks if this interval is before the specified interval.
|
boolean |
isConnected(Interval other) |
Checks if this interval is connected to the specified interval.
|
boolean |
isEmpty() |
Checks if the range is empty.
|
boolean |
isUnboundedEnd() |
Checks if the end of the interval is unbounded.
|
boolean |
isUnboundedStart() |
Checks if the start of the interval is unbounded.
|
static Interval |
of(java.time.Instant startInclusive,
java.time.Duration duration) |
Obtains an instance of
Interval from the start and a duration. |
static Interval |
of(java.time.Instant startInclusive,
java.time.Instant endExclusive) |
Obtains an instance of
Interval from the start and end instant. |
boolean |
overlaps(Interval other) |
Checks if this interval overlaps the specified interval.
|
static Interval |
parse(java.lang.CharSequence text) |
Obtains an instance of
Interval from a text string such as
2007-12-03T10:15:30Z/2007-12-04T10:15:30Z , where the end instant is exclusive. |
Interval |
span(Interval other) |
Calculates the smallest interval that encloses this interval and the specified interval.
|
java.time.Duration |
toDuration() |
Obtains the duration of this interval.
|
java.lang.String |
toString() |
Outputs this interval as a
String , such as 2007-12-03T10:15:30/2007-12-04T10:15:30 . |
Interval |
union(Interval other) |
Calculates the interval that is the union of this interval and the specified interval.
|
Interval |
withEnd(java.time.Instant end) |
Returns a copy of this range with the specified end instant.
|
Interval |
withStart(java.time.Instant start) |
Returns a copy of this range with the specified start instant.
|
public static final Interval ALL
public static Interval of(java.time.Instant startInclusive, java.time.Instant endExclusive)
Interval
from the start and end instant.
The end instant must not be before the start instant.
startInclusive
- the start instant, inclusive, MIN_DATE treated as unbounded, not nullendExclusive
- the end instant, exclusive, MAX_DATE treated as unbounded, not nulljava.time.DateTimeException
- if the end is before the startpublic static Interval of(java.time.Instant startInclusive, java.time.Duration duration)
Interval
from the start and a duration.
The end instant is calculated as the start plus the duration. The duration must not be negative.
startInclusive
- the start instant, inclusive, not nullduration
- the duration from the start to the end, not nulljava.time.DateTimeException
- if the end is before the start,
or if the duration addition cannot be madejava.lang.ArithmeticException
- if numeric overflow occurs when adding the durationpublic static Interval parse(java.lang.CharSequence text)
Interval
from a text string such as
2007-12-03T10:15:30Z/2007-12-04T10:15:30Z
, where the end instant is exclusive.
The string must consist of one of the following four formats:
OffsetDateTime
, followed by a forward slash,
followed by a representation of a OffsetDateTime
OffsetDateTime
, followed by a forward slash,
followed by a representation of a LocalDateTime
, where the end offset is implied.
OffsetDateTime
, followed by a forward slash,
followed by a representation of a PeriodDuration
PeriodDuration
, followed by a forward slash,
followed by a representation of an OffsetDateTime
ISO-8601 supports a very wide range of possible inputs, many of which are not supported here. For example, basic format, week-based dates, ordinal dates and date-style period formats are not supported.
text
- the text to parse, not nulljava.time.format.DateTimeParseException
- if the text cannot be parsedpublic java.time.Instant getStart()
This will return Instant.MIN
if the range is unbounded at the start.
In this case, the range includes all dates into the far-past.
public java.time.Instant getEnd()
This will return Instant.MAX
if the range is unbounded at the end.
In this case, the range includes all dates into the far-future.
public boolean isEmpty()
An empty range occurs when the start date equals the inclusive end date.
public boolean isUnboundedStart()
public boolean isUnboundedEnd()
public Interval withStart(java.time.Instant start)
start
- the start instant for the new interval, not nulljava.time.DateTimeException
- if the resulting interval has end before startpublic Interval withEnd(java.time.Instant end)
end
- the end instant for the new interval, not nulljava.time.DateTimeException
- if the resulting interval has end before startpublic boolean contains(java.time.Instant instant)
This checks if the specified instant is within the bounds of this interval.
If this range has an unbounded start then contains(Instant#MIN)
returns true.
If this range has an unbounded end then contains(Instant#MAX)
returns true.
If this range is empty then this method always returns false.
instant
- the instant, not nullpublic boolean encloses(Interval other)
This checks if the bounds of the specified interval are within the bounds of this interval. An empty interval encloses itself.
other
- the other interval, not nullpublic boolean abuts(Interval other)
The result is true if the end of this interval is the start of the other, or vice versa. An empty interval does not abut itself.
other
- the other interval, not nullpublic boolean isConnected(Interval other)
The result is true if the two intervals have an enclosed interval in common, even if that interval is empty. An empty interval is connected to itself.
This is equivalent to (overlaps(other) || abuts(other))
.
other
- the other interval, not nullpublic boolean overlaps(Interval other)
The result is true if the the two intervals share some part of the time-line. An empty interval overlaps itself.
This is equivalent to (isConnected(other) && !abuts(other))
.
other
- the time interval to compare to, null means a zero length interval nowpublic Interval intersection(Interval other)
This finds the intersection of two intervals. This throws an exception if the two intervals are not connected.
other
- the other interval to check for, not nulljava.time.DateTimeException
- if the intervals do not connectpublic Interval union(Interval other)
This finds the union of two intervals. This throws an exception if the two intervals are not connected.
other
- the other interval to check for, not nulljava.time.DateTimeException
- if the intervals do not connectpublic Interval span(Interval other)
The result of this method will enclose this interval and the specified interval.
other
- the other interval to check for, not nullpublic boolean isAfter(java.time.Instant instant)
The result is true if this instant starts after the specified instant. An empty interval behaves as though it is an instant for comparison purposes.
instant
- the other instant to compare to, not nullpublic boolean isBefore(java.time.Instant instant)
The result is true if this instant ends before the specified instant. Since intervals do not include their end points, this will return true if the instant equals the end of the interval. An empty interval behaves as though it is an instant for comparison purposes.
instant
- the other instant to compare to, not nullpublic boolean isAfter(Interval interval)
The result is true if this instant starts after the end of the specified interval. Since intervals do not include their end points, this will return true if the instant equals the end of the interval. An empty interval behaves as though it is an instant for comparison purposes.
interval
- the other interval to compare to, not nullpublic boolean isBefore(Interval interval)
The result is true if this instant ends before the start of the specified interval. Since intervals do not include their end points, this will return true if the two intervals abut. An empty interval behaves as though it is an instant for comparison purposes.
interval
- the other interval to compare to, not nullpublic java.time.Duration toDuration()
An Interval
is associated with two specific instants on the time-line.
A Duration
is simply an amount of time, separate from the time-line.
java.lang.ArithmeticException
- if the calculation exceeds the capacity of Duration
public boolean equals(java.lang.Object obj)
Compares this Interval
with another ensuring that the two instants are the same.
Only objects of type Interval
are compared, other types return false.
equals
in class java.lang.Object
obj
- the object to check, null returns falsepublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
String
, such as 2007-12-03T10:15:30/2007-12-04T10:15:30
.
The output will be the ISO-8601 format formed by combining the
toString()
methods of the two instants, separated by a forward slash.
toString
in class java.lang.Object
Copyright © 2010–2018 ThreeTen.org. All rights reserved.