Top Description Fields Constructors Methods
org.threeten.extra

public final Class Interval

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, java.time.DateTimeException, .Duration, .Instant, .LocalDateTime, .OffsetDateTime, .ZoneOffset, java.time.format.DateTimeFormatter, .DateTimeParseException, java.time.temporal.TemporalAccessor, java.util.Objects, org.joda.convert.FromString, .ToString

An immutable interval of time between two instants.

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.

Implementation Requirements:

This class is immutable and thread-safe.

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 ==.

Field Summary

Modifier and TypeField and Description
public static final Interval
ALL

An interval over the whole time-line.

private final Instant
end

The end instant (exclusive).

private static final long
serialVersionUID

Serialization version.

private final Instant
start

The start instant (inclusive).

Constructor Summary

AccessConstructor and Description
private
Interval(Instant
the start instant, inclusive, validated not null
startInclusive
,
Instant
the end instant, exclusive, validated not null
endExclusive
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if this interval abuts the other interval
abuts
(Interval
the other interval, not null
other
)

Checks if this interval abuts the specified interval.

public boolean

Returns:

true if this interval contains the instant
contains
(Instant
the instant, not null
instant
)

Checks if this interval contains the specified instant.

public boolean

Returns:

true if this interval contains the other interval
encloses
(Interval
the other interval, not null
other
)

Checks if this interval encloses the specified interval.

public static Interval

Returns:

a new Instant with the specified end instant.
endingAt
(Instant
the end instant, exclusive, not null
endExclusive
)

Obtains an instance of Interval with unbounded start and the specified end instant.

public boolean

Returns:

true if this interval ends after the instant
endsAfter
(Instant
the instant, not null
instant
)

Checks if this interval ends after the specified instant.

public boolean

Returns:

true if this interval ends at or after the instant
endsAtOrAfter
(Instant
the instant, not null
instant
)

Checks if this interval ends after the specified instant.

public boolean

Returns:

true if this interval ends at or before the instant
endsAtOrBefore
(Instant
the instant, not null
instant
)

Checks if this interval ends at or before the specified instant.

public boolean

Returns:

true if this interval ends before the instant
endsBefore
(Instant
the instant, not null
instant
)

Checks if this interval ends before the specified instant.

public boolean

Returns:

true if this is equal to the other interval
equals
(Object
the object to check, null returns false
obj
)

Overrides java.lang.Object.equals.

Checks if this interval is equal to another interval.

public Instant

Returns:

the end of the time interval, exclusive
getEnd
()

Gets the end of this time interval, exclusive.

public Instant

Returns:

the start of the time interval
getStart
()

Gets the start of this time interval, inclusive.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this interval.

public Interval

Returns:

the interval that is the intersection of the two intervals
intersection
(Interval
the other interval to check for, not null
other
)

Calculates the interval that is the intersection of this interval and the specified interval.

public boolean

Returns:

true if this interval is after the specified interval
isAfter
(Interval
the other interval to compare to, not null
interval
)

Checks if this interval is after the specified interval.

public boolean

Returns:

true if the start of this interval is after the specified instant
isAfter
(Instant
the other instant to compare to, not null
instant
)

Checks if this interval is after the specified instant.

public boolean

Returns:

true if this interval is before the specified interval
isBefore
(Interval
the other interval to compare to, not null
interval
)

Checks if this interval is before the specified interval.

public boolean

Returns:

true if the end of this interval is before or equal to the specified instant
isBefore
(Instant
the other instant to compare to, not null
instant
)

Checks if this interval is before the specified instant.

public boolean

Returns:

true if this interval is connected to the other interval
isConnected
(Interval
the other interval, not null
other
)

Checks if this interval is connected to the specified interval.

public boolean

Returns:

true if the range is empty
isEmpty
()

Checks if the range is empty.

public boolean

Returns:

true if end is unbounded
isUnboundedEnd
()

Checks if the end of the interval is unbounded.

public boolean

Returns:

true if start is unbounded
isUnboundedStart
()

Checks if the start of the interval is unbounded.

public static Interval

Returns:

the half-open interval, not null
of
(Instant
the start instant, inclusive, Instant#MIN treated as unbounded, not null
startInclusive
,
Instant
the end instant, exclusive, Instant#MAX treated as unbounded, not null
endExclusive
)

Obtains an instance of Interval from the start and end instant.

public static Interval

Returns:

the interval, not null
of
(Instant
the start instant, inclusive, not null
startInclusive
,
Duration
the duration from the start to the end, not null
duration
)

Obtains an instance of Interval from the start and a duration.

public static Interval

Returns:

the interval, not null
of
(Duration
the duration from the start to the end, not null
duration
,
Instant
the end instant, exclusive, not null
endExclusive
)

Obtains an instance of Interval from the duration and the end.

public boolean

Returns:

true if the time intervals overlap
overlaps
(Interval
the time interval to compare to, null means a zero length interval now
other
)

Checks if this interval overlaps the specified interval.

public static Interval

Returns:

the parsed interval, not null
parse
(CharSequence
the text to parse, not null
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.

private static Interval
private static Interval
private static Interval
public Interval

Returns:

the interval that spans the two intervals
span
(Interval
the other interval to check for, not null
other
)

Calculates the smallest interval that encloses this interval and the specified interval.

public static Interval

Returns:

a new Instant with the specified start instant.
startingAt
(Instant
the start instant, inclusive, not null
startInclusive
)

Obtains an instance of Interval with the specified start instant and unbounded end.

public boolean

Returns:

true if this interval starts after the instant
startsAfter
(Instant
the instant, not null
instant
)

Checks if this interval starts on or after the specified instant.

public boolean

Returns:

true if this interval starts at or after the instant
startsAtOrAfter
(Instant
the instant, not null
instant
)

Checks if this interval starts at or after the specified instant.

public boolean

Returns:

true if this interval starts at or before the instant
startsAtOrBefore
(Instant
the instant, not null
instant
)

Checks if this interval starts at or before the specified instant.

public boolean

Returns:

true if this interval starts before the instant
startsBefore
(Instant
the instant, not null
instant
)

Checks if this interval starts on or before the specified instant.

public Duration

Returns:

the duration of the time interval
toDuration
()

Obtains the duration of this interval.

public String

Returns:

a string representation of this instant, not null
toString
()

Overrides java.lang.Object.toString.

Outputs this interval as a String, such as 2007-12-03T10:15:30/2007-12-04T10:15:30.

public Interval

Returns:

the interval that is the union of the two intervals
union
(Interval
the other interval to check for, not null
other
)

Calculates the interval that is the union of this interval and the specified interval.

public Interval

Returns:

an interval with the start from this interval and the specified end
withEnd
(Instant
the end instant for the new interval, not null
end
)

Returns a copy of this range with the specified end instant.

public Interval

Returns:

an interval with the end from this interval and the specified start
withStart
(Instant
the start instant for the new interval, not null
start
)

Returns a copy of this range with the specified start instant.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ALLback to summary
public static final Interval ALL

An interval over the whole time-line.

endback to summary
private final Instant end

The end instant (exclusive).

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

startback to summary
private final Instant start

The start instant (inclusive).

Constructor Detail

Intervalback to summary
private Interval(Instant startInclusive, Instant endExclusive)

Constructor.

Parameters
startInclusive:Instant

the start instant, inclusive, validated not null

endExclusive:Instant

the end instant, exclusive, validated not null

Method Detail

abutsback to summary
public boolean abuts(Interval other)

Checks if this interval abuts the specified interval.

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.

Parameters
other:Interval

the other interval, not null

Returns:boolean

true if this interval abuts the other interval

containsback to summary
public boolean contains(Instant instant)

Checks if this interval contains the specified instant.

This checks if the specified instant is within the bounds of this interval. If this interval has an unbounded start then contains(Instant#MIN) returns true. If this interval has an unbounded end then contains(Instant#MAX) returns true. Otherwise, if this interval is empty then this method returns false.

This is equivalent to startsAtOrBefore(Instant) && endsAfter(Instant).

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval contains the instant

enclosesback to summary
public boolean encloses(Interval other)

Checks if this interval encloses the specified interval.

This checks if the bounds of the specified interval are within the bounds of this interval. An empty interval encloses itself.

Parameters
other:Interval

the other interval, not null

Returns:boolean

true if this interval contains the other interval

endingAtback to summary
public static Interval endingAt(Instant endExclusive)

Obtains an instance of Interval with unbounded start and the specified end instant.

Parameters
endExclusive:Instant

the end instant, exclusive, not null

Returns:Interval

a new Instant with the specified end instant.

endsAfterback to summary
public boolean endsAfter(Instant instant)

Checks if this interval ends after the specified instant.

This method compares the end of the interval to the instant. An interval with an unbounded end is considered to end after Instant.MAX.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval ends after the instant

endsAtOrAfterback to summary
public boolean endsAtOrAfter(Instant instant)

Checks if this interval ends after the specified instant.

This method compares the end of the interval to the instant. An interval with an unbounded end is considered to end after Instant.MAX.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval ends at or after the instant

endsAtOrBeforeback to summary
public boolean endsAtOrBefore(Instant instant)

Checks if this interval ends at or before the specified instant.

This method compares the end of the interval to the instant. An interval with an unbounded end is considered to end after Instant.MAX.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval ends at or before the instant

endsBeforeback to summary
public boolean endsBefore(Instant instant)

Checks if this interval ends before the specified instant.

This method compares the end of the interval to the instant. An interval with an unbounded end is considered to end after Instant.MAX.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval ends before the instant

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Checks if this interval is equal to another interval.

Compares this Interval with another ensuring that the two instants are the same. Only objects of type Interval are compared, other types return false.

Parameters
obj:Object

the object to check, null returns false

Returns:boolean

true if this is equal to the other interval

Annotations
@Override
getEndback to summary
public Instant getEnd()

Gets the end of this time interval, exclusive.

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.

Returns:Instant

the end of the time interval, exclusive

getStartback to summary
public Instant getStart()

Gets the start of this time interval, inclusive.

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.

Returns:Instant

the start of the time interval

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

A hash code for this interval.

Returns:int

a suitable hash code

Annotations
@Override
intersectionback to summary
public Interval intersection(Interval other)

Calculates the interval that is the intersection of this interval and the specified interval.

This finds the intersection of two intervals. This throws an exception if the two intervals are not connected.

Parameters
other:Interval

the other interval to check for, not null

Returns:Interval

the interval that is the intersection of the two intervals

Exceptions
DateTimeException:
if the intervals do not connect
isAfterback to summary
public boolean isAfter(Interval interval)

Checks if this interval is after the specified interval.

The result is true if this interval starts after the end 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.

Parameters
interval:Interval

the other interval to compare to, not null

Returns:boolean

true if this interval is after the specified interval

isAfterback to summary
public boolean isAfter(Instant instant)

Checks if this interval is after the specified instant.

The result is true if this interval starts after the specified instant. An empty interval behaves as though it is an instant for comparison purposes.

This is equivalent to startsAfter(Instant).

Parameters
instant:Instant

the other instant to compare to, not null

Returns:boolean

true if the start of this interval is after the specified instant

isBeforeback to summary
public boolean isBefore(Interval interval)

Checks if this interval is before the specified interval.

The result is true if this interval 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.

Parameters
interval:Interval

the other interval to compare to, not null

Returns:boolean

true if this interval is before the specified interval

isBeforeback to summary
public boolean isBefore(Instant instant)

Checks if this interval is before the specified instant.

The result is true if this interval 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.

This is equivalent to endsAtOrBefore(Instant) && startsBefore(Instant).

Parameters
instant:Instant

the other instant to compare to, not null

Returns:boolean

true if the end of this interval is before or equal to the specified instant

isConnectedback to summary
public boolean isConnected(Interval other)

Checks if this interval is connected to the specified interval.

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)).

Parameters
other:Interval

the other interval, not null

Returns:boolean

true if this interval is connected to the other interval

isEmptyback to summary
public boolean isEmpty()

Checks if the range is empty.

An empty range occurs when the start date equals the inclusive end date.

Returns:boolean

true if the range is empty

isUnboundedEndback to summary
public boolean isUnboundedEnd()

Checks if the end of the interval is unbounded.

Returns:boolean

true if end is unbounded

isUnboundedStartback to summary
public boolean isUnboundedStart()

Checks if the start of the interval is unbounded.

Returns:boolean

true if start is unbounded

ofback to summary
public static Interval of(Instant startInclusive, Instant endExclusive)

Obtains an instance of Interval from the start and end instant.

The end instant must not be before the start instant.

Parameters
startInclusive:Instant

the start instant, inclusive, Instant#MIN treated as unbounded, not null

endExclusive:Instant

the end instant, exclusive, Instant#MAX treated as unbounded, not null

Returns:Interval

the half-open interval, not null

Exceptions
DateTimeException:
if the end is before the start
ofback to summary
public static Interval of(Instant startInclusive, Duration duration)

Obtains an instance of Interval from the start and a duration.

The end instant is calculated as the start plus the duration. The duration must not be negative.

Parameters
startInclusive:Instant

the start instant, inclusive, not null

duration:Duration

the duration from the start to the end, not null

Returns:Interval

the interval, not null

Exceptions
DateTimeException:
if the end is before the start, or if the duration addition cannot be made
ArithmeticException:
if numeric overflow occurs when adding the duration
ofback to summary
public static Interval of(Duration duration, Instant endExclusive)

Obtains an instance of Interval from the duration and the end.

The start instant is calculated as the end minus the duration. The duration must not be negative.

Parameters
duration:Duration

the duration from the start to the end, not null

endExclusive:Instant

the end instant, exclusive, not null

Returns:Interval

the interval, not null

Exceptions
DateTimeException:
if the end is before the start, or if the duration addition cannot be made
ArithmeticException:
if numeric overflow occurs when subtracting the duration
overlapsback to summary
public boolean overlaps(Interval other)

Checks if this interval overlaps the specified interval.

The result is true if the two intervals share some part of the time-line. An empty interval overlaps itself.

This is equivalent to (isConnected(other) && !abuts(other)).

Parameters
other:Interval

the time interval to compare to, null means a zero length interval now

Returns:boolean

true if the time intervals overlap

parseback to summary
public 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, where the end instant is exclusive.

The string must consist of one of the following four formats:

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.

Parameters
text:CharSequence

the text to parse, not null

Returns:Interval

the parsed interval, not null

Annotations
@FromString
Exceptions
DateTimeParseException:
if the text cannot be parsed
parseEndDateTimeback to summary
private static Interval parseEndDateTime(Instant start, ZoneOffset offset, CharSequence endStr)
parseSplitback to summary
private static Interval parseSplit(CharSequence startStr, CharSequence endStr)
parseStartExtendedback to summary
private static Interval parseStartExtended(CharSequence startStr, CharSequence endStr)
spanback to summary
public Interval span(Interval other)

Calculates the smallest interval that encloses this interval and the specified interval.

The result of this method will enclose this interval and the specified interval.

Parameters
other:Interval

the other interval to check for, not null

Returns:Interval

the interval that spans the two intervals

startingAtback to summary
public static Interval startingAt(Instant startInclusive)

Obtains an instance of Interval with the specified start instant and unbounded end.

Parameters
startInclusive:Instant

the start instant, inclusive, not null

Returns:Interval

a new Instant with the specified start instant.

startsAfterback to summary
public boolean startsAfter(Instant instant)

Checks if this interval starts on or after the specified instant.

This method compares the start of the interval to the instant. An interval with an unbounded start is considered to start at Instant.MIN.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval starts after the instant

startsAtOrAfterback to summary
public boolean startsAtOrAfter(Instant instant)

Checks if this interval starts at or after the specified instant.

This method compares the start of the interval to the instant. An interval with an unbounded start is considered to start at Instant.MIN.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval starts at or after the instant

startsAtOrBeforeback to summary
public boolean startsAtOrBefore(Instant instant)

Checks if this interval starts at or before the specified instant.

This method compares the start of the interval to the instant. An interval with an unbounded start is considered to start at Instant.MIN.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval starts at or before the instant

startsBeforeback to summary
public boolean startsBefore(Instant instant)

Checks if this interval starts on or before the specified instant.

This method compares the start of the interval to the instant. An interval with an unbounded start is considered to start at Instant.MIN.

Parameters
instant:Instant

the instant, not null

Returns:boolean

true if this interval starts before the instant

toDurationback to summary
public Duration toDuration()

Obtains the duration of this interval.

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.

Returns:Duration

the duration of the time interval

Exceptions
ArithmeticException:
if the calculation exceeds the capacity of Duration
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Outputs this interval as a 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.

Returns:String

a string representation of this instant, not null

Annotations
@Override
@ToString
unionback to summary
public Interval union(Interval other)

Calculates the interval that is the union of this interval and the specified interval.

This finds the union of two intervals. This throws an exception if the two intervals are not connected.

Parameters
other:Interval

the other interval to check for, not null

Returns:Interval

the interval that is the union of the two intervals

Exceptions
DateTimeException:
if the intervals do not connect
withEndback to summary
public Interval withEnd(Instant end)

Returns a copy of this range with the specified end instant.

Parameters
end:Instant

the end instant for the new interval, not null

Returns:Interval

an interval with the start from this interval and the specified end

Exceptions
DateTimeException:
if the resulting interval has end before start
withStartback to summary
public Interval withStart(Instant start)

Returns a copy of this range with the specified start instant.

Parameters
start:Instant

the start instant for the new interval, not null

Returns:Interval

an interval with the end from this interval and the specified start

Exceptions
DateTimeException:
if the resulting interval has end before start