Top Description Fields Constructors Methods
org.threeten.extra

public final Class LocalDateRange

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, java.time.DateTimeException, .LocalDate, .Period, java.time.format.DateTimeParseException, java.time.temporal.TemporalAdjuster, java.util.Comparator, .Objects, .Spliterator, .Spliterators, java.util.function.Consumer, java.util.stream.Stream, .StreamSupport, org.joda.convert.FromString, .ToString

A range of local dates.

A LocalDateRange represents a range of dates, from a start date to an end date. Instances can be constructed from either a half-open or a closed range of dates. Internally, the class stores the start and end dates, with the start inclusive and the end exclusive. The end date is always greater than or equal to the start date.

The constants LocalDate.MIN and LocalDate.MAX can be used to indicate an unbounded far-past or far-future. Note that there is no difference between a half-open and a closed range when the end is LocalDate.MAX. Empty ranges are allowed.

No range can end at LocalDate.MIN or LocalDate.MIN.plusDays(1). No range can start at LocalDate.MAX or LocalDate.MAX.minusDays(1). No empty range can exist at LocalDate.MIN or LocalDate.MAX.

Date ranges are not comparable. To compare the length of two ranges, it is generally recommended to compare the number of days they contain.

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 LocalDateRange
ALL

A range over the whole time-line.

private final LocalDate
end

The end date (exclusive).

private static final LocalDate
MAXM1

The day before the MAX date.

private static final LocalDate
MINP1

The day after the MIN date.

private static final long
serialVersionUID

Serialization version.

private final LocalDate
start

The start date (inclusive).

Constructor Summary

AccessConstructor and Description
private
LocalDateRange(LocalDate
the start date, inclusive, validated not null
startInclusive
,
LocalDate
the end date, exclusive, validated not null
endExclusive
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if this range abuts the other range
abuts
(LocalDateRange
the other range, not null
other
)

Checks if this range abuts the specified range.

public boolean

Returns:

true if this range contains the date
contains
(LocalDate
the date to check for, not null
date
)

Checks if this range contains the specified date.

public boolean

Returns:

true if this range contains all dates in the other range
encloses
(LocalDateRange
the other range to check for, not null
other
)

Checks if this range encloses the specified range.

public boolean

Returns:

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

Overrides java.lang.Object.equals.

Checks if this range is equal to another range.

public LocalDate

Returns:

the end date, exclusive
getEnd
()

Gets the end date of this range, exclusive.

public LocalDate

Returns:

the end date, inclusive
getEndInclusive
()

Gets the end date of this range, inclusive.

public LocalDate

Returns:

the start date
getStart
()

Gets the start date of this range, inclusive.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this range.

public LocalDateRange

Returns:

the range that is the intersection of the two ranges
intersection
(LocalDateRange
the other range to check for, not null
other
)

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

public boolean

Returns:

true if the start of this range is after the specified date
isAfter
(LocalDate
the other date to compare to, not null
date
)

Checks if this range is after the specified date.

public boolean

Returns:

true if every date in this range is after every date in the other range
isAfter
(LocalDateRange
the other range to compare to, not null
other
)

Checks if this range is after the specified range.

public boolean

Returns:

true if the start of this range is before the specified date
isBefore
(LocalDate
the other date to compare to, not null
date
)

Checks if this range is before the specified date.

public boolean

Returns:

true if every date in this range is before every date in the other range
isBefore
(LocalDateRange
the other range to compare to, not null
range
)

Checks if this range is before the specified range.

public boolean

Returns:

true if this range is connected to the other range
isConnected
(LocalDateRange
the other range, not null
other
)

Checks if this range is connected to the specified range.

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 range is unbounded.

public boolean

Returns:

true if start is unbounded
isUnboundedStart
()

Checks if the start of the range is unbounded.

public int

Returns:

the length in days, Integer.MAX_VALUE if unbounded or too large
lengthInDays
()

Obtains the length of this range in days.

public static LocalDateRange

Returns:

the half-open range, not null
of
(LocalDate
the inclusive start date, not null
startInclusive
,
LocalDate
the exclusive end date, not null
endExclusive
)

Obtains a half-open range of dates, including the start and excluding the end.

public static LocalDateRange

Returns:

the range, not null
of
(LocalDate
the inclusive start date, not null
startInclusive
,
Period
the period from the start to the end, not null
period
)

Obtains an instance of LocalDateRange from the start and a period.

public static LocalDateRange

Returns:

the closed range
ofClosed
(LocalDate
the inclusive start date, not null
startInclusive
,
LocalDate
the inclusive end date, not null
endInclusive
)

Obtains a closed range of dates, including the start and end.

public static LocalDateRange

Returns:

the empty range, not null
ofEmpty
(LocalDate
the date where the empty range is located, not null
date
)

Obtains an empty date range located at the specified date.

public static LocalDateRange

Returns:

the range, with an unbounded start and unbounded end
ofUnbounded
()

Obtains a range that is unbounded at the start and end.

public static LocalDateRange

Returns:

the range, with an unbounded end
ofUnboundedEnd
(LocalDate
the inclusive start date, LocalDate.MIN treated as unbounded, not null
startInclusive
)

Obtains a range from and including the specified start date.

public static LocalDateRange

Returns:

the range, with an unbounded start
ofUnboundedStart
(LocalDate
the exclusive end date, LocalDate.MAX treated as unbounded, not null
endExclusive
)

Obtains a range up to, but not including, the specified end date.

public boolean

Returns:

true if the time ranges overlap
overlaps
(LocalDateRange
the time range to compare to, null means a zero length range now
other
)

Checks if this range overlaps the specified range.

public static LocalDateRange

Returns:

the parsed range, not null
parse
(CharSequence
the text to parse, not null
text
)

Obtains an instance of LocalDateRange from a text string such as 2007-12-03/2007-12-04, where the end date is exclusive.

public LocalDateRange

Returns:

the range that spans the two ranges
span
(LocalDateRange
the other range to check for, not null
other
)

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

public Stream<LocalDate>

Returns:

the stream of dates from the start to the end
stream
()

Streams the set of dates included in the range.

public Period

Returns:

the period of the range
toPeriod
()

Obtains the length of this range as a period.

public String

Returns:

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

Overrides java.lang.Object.toString.

Outputs this range as a String, such as 2007-12-03/2007-12-04.

public LocalDateRange

Returns:

the range that is the union of the two ranges
union
(LocalDateRange
the other range to check for, not null
other
)

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

public LocalDateRange

Returns:

a copy of this range with the end date adjusted
withEnd
(TemporalAdjuster
the adjuster to use, not null
adjuster
)

Returns a copy of this range with the end date adjusted.

public LocalDateRange

Returns:

a copy of this range with the start date adjusted
withStart
(TemporalAdjuster
the adjuster to use, not null
adjuster
)

Returns a copy of this range with the start date adjusted.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ALLback to summary
public static final LocalDateRange ALL

A range over the whole time-line.

endback to summary
private final LocalDate end

The end date (exclusive).

MAXM1back to summary
private static final LocalDate MAXM1

The day before the MAX date.

MINP1back to summary
private static final LocalDate MINP1

The day after the MIN date.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

startback to summary
private final LocalDate start

The start date (inclusive).

Constructor Detail

LocalDateRangeback to summary
private LocalDateRange(LocalDate startInclusive, LocalDate endExclusive)

Constructor.

Parameters
startInclusive:LocalDate

the start date, inclusive, validated not null

endExclusive:LocalDate

the end date, exclusive, validated not null

Method Detail

abutsback to summary
public boolean abuts(LocalDateRange other)

Checks if this range abuts the specified range.

The result is true if the end of this range is the start of the other, or vice versa. An empty range does not abut itself.

Parameters
other:LocalDateRange

the other range, not null

Returns:boolean

true if this range abuts the other range

containsback to summary
public boolean contains(LocalDate date)

Checks if this range contains the specified date.

This checks if the specified date is within the bounds of this range. If this range is empty then this method always returns false. Else if this range has an unbounded start then contains(LocalDate#MIN) returns true. Else if this range has an unbounded end then contains(LocalDate#MAX) returns true.

Parameters
date:LocalDate

the date to check for, not null

Returns:boolean

true if this range contains the date

enclosesback to summary
public boolean encloses(LocalDateRange other)

Checks if this range encloses the specified range.

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

Parameters
other:LocalDateRange

the other range to check for, not null

Returns:boolean

true if this range contains all dates in the other range

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Checks if this range is equal to another range.

Compares this LocalDateRange with another ensuring that the two dates are the same. Only objects of type LocalDateRange 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 range

Annotations
@Override
getEndback to summary
public LocalDate getEnd()

Gets the end date of this range, exclusive.

This will return LocalDate.MAX if the range is unbounded at the end. In this case, the range includes all dates into the far-future.

This never returns LocalDate.MIN or LocalDate.MIN.plusDays(1).

Returns:LocalDate

the end date, exclusive

getEndInclusiveback to summary
public LocalDate getEndInclusive()

Gets the end date of this range, inclusive.

This will return LocalDate.MAX if the range is unbounded at the end. In this case, the range includes all dates into the far-future.

This returns the date before the end date.

This never returns LocalDate.MIN.

Returns:LocalDate

the end date, inclusive

getStartback to summary
public LocalDate getStart()

Gets the start date of this range, inclusive.

This will return LocalDate#MIN if the range is unbounded at the start. In this case, the range includes all dates into the far-past.

This never returns LocalDate.MAX or LocalDate.MAX.minusDays(1).

Returns:LocalDate

the start date

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

A hash code for this range.

Returns:int

a suitable hash code

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

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

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

Parameters
other:LocalDateRange

the other range to check for, not null

Returns:LocalDateRange

the range that is the intersection of the two ranges

Exceptions
DateTimeException:
if the ranges do not connect
isAfterback to summary
public boolean isAfter(LocalDate date)

Checks if this range is after the specified date.

The result is true if every date in this range is after the specified date. An empty range behaves as though it is a date for comparison purposes.

Parameters
date:LocalDate

the other date to compare to, not null

Returns:boolean

true if the start of this range is after the specified date

isAfterback to summary
public boolean isAfter(LocalDateRange other)

Checks if this range is after the specified range.

The result is true if every date in this range is after every date in the specified range. An empty range behaves as though it is a date for comparison purposes.

Parameters
other:LocalDateRange

the other range to compare to, not null

Returns:boolean

true if every date in this range is after every date in the other range

isBeforeback to summary
public boolean isBefore(LocalDate date)

Checks if this range is before the specified date.

The result is true if every date in this range is before the specified date. An empty range behaves as though it is a date for comparison purposes.

Parameters
date:LocalDate

the other date to compare to, not null

Returns:boolean

true if the start of this range is before the specified date

isBeforeback to summary
public boolean isBefore(LocalDateRange range)

Checks if this range is before the specified range.

The result is true if every date in this range is before every date in the specified range. An empty range behaves as though it is a date for comparison purposes.

Parameters
range:LocalDateRange

the other range to compare to, not null

Returns:boolean

true if every date in this range is before every date in the other range

isConnectedback to summary
public boolean isConnected(LocalDateRange other)

Checks if this range is connected to the specified range.

The result is true if the two ranges have an enclosed range in common, even if that range is empty. An empty range is connected to itself.

This is equivalent to (overlaps(other) || abuts(other)).

Parameters
other:LocalDateRange

the other range, not null

Returns:boolean

true if this range is connected to the other range

isEmptyback to summary
public boolean isEmpty()

Checks if the range is empty.

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

An empty range is never unbounded.

Returns:boolean

true if the range is empty

isUnboundedEndback to summary
public boolean isUnboundedEnd()

Checks if the end of the range is unbounded.

An unbounded range is never empty.

Returns:boolean

true if end is unbounded

isUnboundedStartback to summary
public boolean isUnboundedStart()

Checks if the start of the range is unbounded.

An unbounded range is never empty.

Returns:boolean

true if start is unbounded

lengthInDaysback to summary
public int lengthInDays()

Obtains the length of this range in days.

This returns the number of days between the start and end dates. If the range is too large, the length will be Integer.MAX_VALUE. Unbounded ranges return Integer.MAX_VALUE.

Returns:int

the length in days, Integer.MAX_VALUE if unbounded or too large

ofback to summary
public static LocalDateRange of(LocalDate startInclusive, LocalDate endExclusive)

Obtains a half-open range of dates, including the start and excluding the end.

The range includes the start date and excludes the end date, unless the end is LocalDate.MAX. The end date must be equal to or after the start date. This definition permits an empty range located at a specific date.

The constants LocalDate.MIN and LocalDate.MAX can be used to indicate an unbounded far-past or far-future.

The start inclusive date must not be LocalDate.MAX or LocalDate.MAX.minusDays(1). The end inclusive date must not be LocalDate.MIN or LocalDate.MIN.plusDays(1). No empty range can exist at LocalDate.MIN or LocalDate.MAX.

Parameters
startInclusive:LocalDate

the inclusive start date, not null

endExclusive:LocalDate

the exclusive end date, not null

Returns:LocalDateRange

the half-open range, not null

Exceptions
DateTimeException:
if the end is before the start, or the start date is LocalDate.MAX or LocalDate.MAX.minusDays(1), or the end date is LocalDate.MIN or LocalDate.MIN.plusDays(1)
ofback to summary
public static LocalDateRange of(LocalDate startInclusive, Period period)

Obtains an instance of LocalDateRange from the start and a period.

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

The constant LocalDate.MIN can be used to indicate an unbounded far-past.

The period must not be zero or one day when the start date is LocalDate.MIN.

Parameters
startInclusive:LocalDate

the inclusive start date, not null

period:Period

the period from the start to the end, not null

Returns:LocalDateRange

the range, not null

Exceptions
DateTimeException:
if the end is before the start, or if the period addition cannot be made
ArithmeticException:
if numeric overflow occurs when adding the period
ofClosedback to summary
public static LocalDateRange ofClosed(LocalDate startInclusive, LocalDate endInclusive)

Obtains a closed range of dates, including the start and end.

The range includes the start date and the end date. The end date must be equal to or after the start date.

The constants LocalDate.MIN and LocalDate.MAX can be used to indicate an unbounded far-past or far-future. In addition, an end date of LocalDate.MAX.minusDays(1) will also create an unbounded far-future range.

The start inclusive date must not be LocalDate.MAX or LocalDate.MAX.minusDays(1). The end inclusive date must not be LocalDate.MIN.

Parameters
startInclusive:LocalDate

the inclusive start date, not null

endInclusive:LocalDate

the inclusive end date, not null

Returns:LocalDateRange

the closed range

Exceptions
DateTimeException:
if the end is before the start, or the start date is LocalDate.MAX or LocalDate.MAX.minusDays(1), or the end date is LocalDate.MIN
ofEmptyback to summary
public static LocalDateRange ofEmpty(LocalDate date)

Obtains an empty date range located at the specified date.

The empty range has zero length and contains no other dates or ranges. An empty range cannot be located at LocalDate.MIN, LocalDate.MIN.plusDays(1), LocalDate.MAX or LocalDate.MAX.minusDays(1).

Parameters
date:LocalDate

the date where the empty range is located, not null

Returns:LocalDateRange

the empty range, not null

Exceptions
DateTimeException:
if the date is LocalDate.MIN, LocalDate.MIN.plusDays(1), LocalDate.MAX or LocalDate.MAX.minusDays(1)
ofUnboundedback to summary
public static LocalDateRange ofUnbounded()

Obtains a range that is unbounded at the start and end.

Returns:LocalDateRange

the range, with an unbounded start and unbounded end

ofUnboundedEndback to summary
public static LocalDateRange ofUnboundedEnd(LocalDate startInclusive)

Obtains a range from and including the specified start date.

The range includes all dates from the start date to the unbounded end, denoted by LocalDate.MAX. The start date is inclusive and cannot be LocalDate.MAX or LocalDate.MAX.minusDays(1).

Parameters
startInclusive:LocalDate

the inclusive start date, LocalDate.MIN treated as unbounded, not null

Returns:LocalDateRange

the range, with an unbounded end

Exceptions
DateTimeException:
if the start date is LocalDate.MAX or LocalDate.MAX.minusDays(1)
ofUnboundedStartback to summary
public static LocalDateRange ofUnboundedStart(LocalDate endExclusive)

Obtains a range up to, but not including, the specified end date.

The range includes all dates from the unbounded start, denoted by LocalDate.MIN, to the end date. The end date is exclusive and cannot be LocalDate.MIN or LocalDate.MIN.plusDays(1).

Parameters
endExclusive:LocalDate

the exclusive end date, LocalDate.MAX treated as unbounded, not null

Returns:LocalDateRange

the range, with an unbounded start

Exceptions
DateTimeException:
if the end date is LocalDate.MIN or LocalDate.MIN.plusDays(1)
overlapsback to summary
public boolean overlaps(LocalDateRange other)

Checks if this range overlaps the specified range.

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

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

Parameters
other:LocalDateRange

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

Returns:boolean

true if the time ranges overlap

parseback to summary
public static LocalDateRange parse(CharSequence text)

Obtains an instance of LocalDateRange from a text string such as 2007-12-03/2007-12-04, where the end date is exclusive.

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

  • a representations of an LocalDate, followed by a forward slash, followed by a representation of a LocalDate
  • a representation of an LocalDate, followed by a forward slash, followed by a representation of a Period
  • a representation of a Period, followed by a forward slash, followed by a representation of an LocalDate
Parameters
text:CharSequence

the text to parse, not null

Returns:LocalDateRange

the parsed range, not null

Annotations
@FromString
Exceptions
DateTimeParseException:
if the text cannot be parsed
spanback to summary
public LocalDateRange span(LocalDateRange other)

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

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

Parameters
other:LocalDateRange

the other range to check for, not null

Returns:LocalDateRange

the range that spans the two ranges

streamback to summary
public Stream<LocalDate> stream()

Streams the set of dates included in the range.

This returns a stream consisting of each date in the range. The stream is ordered.

Returns:Stream<LocalDate>

the stream of dates from the start to the end

toPeriodback to summary
public Period toPeriod()

Obtains the length of this range as a period.

This returns the Period between the start and end dates. Unbounded ranges throw ArithmeticException.

Returns:Period

the period of the range

Exceptions
ArithmeticException:
if the calculation exceeds the capacity of Period, or the range is unbounded
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Outputs this range as a String, such as 2007-12-03/2007-12-04.

The output will be the ISO-8601 format formed by combining the toString() methods of the two dates, separated by a forward slash.

Returns:String

a string representation of this date, not null

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

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

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

Parameters
other:LocalDateRange

the other range to check for, not null

Returns:LocalDateRange

the range that is the union of the two ranges

Exceptions
DateTimeException:
if the ranges do not connect
withEndback to summary
public LocalDateRange withEnd(TemporalAdjuster adjuster)

Returns a copy of this range with the end date adjusted.

This returns a new instance with the exclusive end date altered. Since LocalDate implements TemporalAdjuster any local date can simply be passed in.

For example, to adjust the end to one week later:

 range = range.withEnd(date -> date.plus(1, ChronoUnit.WEEKS));
Parameters
adjuster:TemporalAdjuster

the adjuster to use, not null

Returns:LocalDateRange

a copy of this range with the end date adjusted

Exceptions
DateTimeException:
if the new end date is before the current start date
withStartback to summary
public LocalDateRange withStart(TemporalAdjuster adjuster)

Returns a copy of this range with the start date adjusted.

This returns a new instance with the start date altered. Since LocalDate implements TemporalAdjuster any local date can simply be passed in.

For example, to adjust the start to one week earlier:

 range = range.withStart(date -> date.minus(1, ChronoUnit.WEEKS));
Parameters
adjuster:TemporalAdjuster

the adjuster to use, not null

Returns:LocalDateRange

a copy of this range with the start date adjusted

Exceptions
DateTimeException:
if the new start date is after the current end date