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(java.time.Instant instant)
Checks if this interval contain 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.
|
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.
|
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 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. |
java.time.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(java.time.Instant end)
Creates a new interval with the specified end instant.
|
Interval |
withStart(java.time.Instant start)
Creates a new interval with the specified start instant.
|
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, not nullendExclusive - the end instant, exclusive, 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 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 two valid string representations of an Instant
separated by a forward slash.
text - the text to parse, not nulljava.time.format.DateTimeParseException - if the text cannot be parsedpublic java.time.Instant getStart()
public java.time.Instant getEnd()
public Interval withStart(java.time.Instant start)
start - the start instant for the new interval, not nullIllegalArgumentException - if the resulting interval has end before startpublic Interval withEnd(java.time.Instant end)
end - the end instant for the new interval, not nullIllegalArgumentException - if the resulting interval has end before startpublic boolean contains(java.time.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 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.
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–2014 ThreeTen.org. All rights reserved.