public final class Interval extends Object implements Serializable
An interval represents the time on the time-line between two Instants.
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 | Method and Description |
|---|---|
boolean |
abuts(Interval interval)
Checks if this interval abuts the specified interval.
|
boolean |
contains(Instant instant)
Checks if this interval contains the specified instant.
|
boolean |
encloses(Interval interval)
Checks if this interval encloses the specified interval.
|
boolean |
equals(Object obj)
Checks if this interval is equal to another interval.
|
Instant |
getEnd()
Gets the end of this time interval, exclusive.
|
Instant |
getStart()
Gets the start of this time interval, inclusive.
|
int |
hashCode()
A hash code for this interval.
|
boolean |
isAfter(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(Instant instant)
Checks if this interval is before the specified instant.
|
boolean |
isBefore(Interval interval)
Checks if this interval is before the specified interval.
|
static Interval |
of(Instant startInclusive,
Duration duration)
Obtains an instance of
Interval from the start and a duration. |
static Interval |
of(Instant startInclusive,
Instant endExclusive)
Obtains an instance of
Interval from the start and end instant. |
boolean |
overlaps(Interval interval)
Checks if this interval overlaps the specified interval.
|
static Interval |
parse(CharSequence text)
Obtains an instance of
Interval from a text string such as
2007-12-03T10:15:30Z/2007-12-04T10:15:30Z. |
Duration |
toDuration()
Obtains the duration of this interval.
|
String |
toString()
Outputs this interval as a
String, such as 2007-12-03T10:15:30/2007-12-04T10:15:30. |
Interval |
withEnd(Instant end)
Creates a new interval with the specified end instant.
|
Interval |
withStart(Instant start)
Creates a new interval with the specified start instant.
|
public static Interval of(Instant startInclusive, Instant endExclusive)
Interval from the start and end instant.
The end instant must not be before the start instant.
startInclusive - the start instant, inclusive, not nullendExclusive - the end instant, exclusive, not nullDateTimeException - if the end is before the startpublic static Interval of(Instant startInclusive, 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 nullDateTimeException - if the end is before the start,
or if the duration addition cannot be madeArithmeticException - if numeric overflow occurs when adding the durationpublic static Interval parse(CharSequence text)
Interval from a text string such as
2007-12-03T10:15:30Z/2007-12-04T10:15:30Z.
The string must consist of one of the following three formats:
Instant, followed by a forward slash, followed by a representation of a Instant
Instant, followed by a forward slash, followed by a representation of a Duration
Duration, followed by a forward slash, followed by a representation of an Instant
text - the text to parse, not nullDateTimeParseException - if the text cannot be parsedpublic Instant getStart()
public Instant getEnd()
public Interval withStart(Instant start)
start - the start instant for the new interval, not nullIllegalArgumentException - if the resulting interval has end before startpublic Interval withEnd(Instant end)
end - the end instant for the new interval, not nullIllegalArgumentException - if the resulting interval has end before startpublic boolean contains(Instant instant)
The result is true if the instant is equal or after the start and before the end. An empty interval does not contain anything.
instant - the instant, not nullpublic boolean encloses(Interval interval)
This checks if the specified interval is fully enclosed by this interval. The result is true if the start of the specified interval is contained in this interval, and the end is contained or equal to the end of this interval. An empty interval contains an equal empty interval, but no other intervals.
interval - the interval, not nullpublic boolean overlaps(Interval interval)
The result is true if the the two intervals share some part of the time-line. An empty interval overlaps an equal empty interval.
interval - the time interval to compare to, null means a zero length interval nowpublic boolean abuts(Interval interval)
The result is true if the the two intervals have one instant in common. An empty interval does not abut an equal empty interval.
interval - the interval, not nullpublic boolean isAfter(Instant instant)
The result is true if the 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(Instant instant)
The result is true if the 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 the 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 the 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 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.
ArithmeticException - if the calculation exceeds the capacity of Durationpublic boolean equals(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.
public int hashCode()
public 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.
Copyright © 2010–2016 ThreeTen.org. All rights reserved.