Home Reference Source
public class | source

LocalDateRange

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.

Static Method Summary

Static Public Methods
public static

of(startInclusive: LocalDate, endExclusiveOrPeriod: LocalDate | Period): LocalDateRange

function overloading for LocalDateRange.of

public static

ofClosed(startInclusive: LocalDate, endInclusive: LocalDate): LocalDateRange

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

public static

Obtains an empty date range located at the specified date.

public static

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

public static

Obtains a range from and including the specified start date.

public static

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

public static

parse(text: string): LocalDateRange

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

Static Protected Methods
protected static

_ofLocalDateLocalDate(startInclusive: LocalDate, endExclusive: LocalDate): LocalDateRange

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

protected static

_ofLocalDatePeriod(startInclusive: LocalDate, period: Period): LocalDateRange

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

Method Summary

Public Methods
public

abuts(other: LocalDateRange): boolean

Checks if this range abuts the specified range.

public

contains(date: LocalDate): boolean

Checks if this range contains the specified date.

public

encloses(other: LocalDateRange): boolean

Checks if this range encloses the specified range.

public

Gets the end date of this range, exclusive.

public

Gets the end date of this range, inclusive.

public

equals(obj: *): boolean

Checks if this range is equal to another range.

public

hashCode(): number

A hash code for this range.

public

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

public

isAfter(localDateOrLocalDateRange: LocalDate | LocalDateRange): boolean

Function overloading for LocalDateRange.isAfter

public

isBefore(localDateOrLocalDateRange: LocalDate | LocalDateRange): boolean

Function overloading for LocalDateRange.isBefore

public

isConnected(other: LocalDateRange): boolean

Checks if this range is connected to the specified range.

public

isEmpty(): boolean

Checks if the range is empty.

public

isUnboundedEnd(): boolean

Checks if the end of the range is unbounded.

public

isUnboundedStart(): boolean

Checks if the start of the range is unbounded.

public

lengthInDays(): number

Obtains the length of this range in days.

public

overlaps(other: LocalDateRange): boolean

Checks if this range overlaps the specified range.

public

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

public

Gets the start date of this range, inclusive.

public

Obtains the length of this range as a period.

public

toString(): string

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

public

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

public

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

public

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

Protected Methods
protected

_isAfterLocalDate(date: LocalDate): boolean

Checks if this range is after the specified date.

protected

Checks if this range is after the specified range.

protected

_isBeforeLocalDate(date: LocalDate): boolean

Checks if this range is before the specified date.

protected

Checks if this range is before the specified range.

Static Public Methods

public static of(startInclusive: LocalDate, endExclusiveOrPeriod: LocalDate | Period): LocalDateRange source

function overloading for LocalDateRange.of

Params:

NameTypeAttributeDescription
startInclusive LocalDate
endExclusiveOrPeriod LocalDate | Period

Return:

LocalDateRange

public static ofClosed(startInclusive: LocalDate, endInclusive: LocalDate): LocalDateRange source

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.

Params:

NameTypeAttributeDescription
startInclusive LocalDate

the inclusive start date, not null

endInclusive LocalDate

the inclusive end date, not null

Return:

LocalDateRange

the closed range

Throw:

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

public static ofEmpty(date: LocalDate): LocalDateRange source

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

Params:

NameTypeAttributeDescription
date LocalDate

the date where the empty range is located, not null

Return:

LocalDateRange

the empty range, not null

Throw:

DateTimeException

if the date is LocalDate.MIN, LocalDate.MIN.plusDays(1), LocalDate.MAX or LocalDate.MAX.minusDays(1)

public static ofUnbounded(): LocalDateRange source

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

Return:

LocalDateRange

the range, with an unbounded start and unbounded end

public static ofUnboundedEnd(startInclusive: LocalDate): LocalDateRange source

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

Params:

NameTypeAttributeDescription
startInclusive LocalDate

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

Return:

LocalDateRange

the range, with an unbounded end

Throw:

DateTimeException

if the start date is LocalDate.MAX or LocalDate.MAX.minusDays(1)

public static ofUnboundedStart(endExclusive: LocalDate): LocalDateRange source

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

Params:

NameTypeAttributeDescription
endExclusive LocalDate

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

Return:

LocalDateRange

the range, with an unbounded start

Throw:

DateTimeException

if the end date is LocalDate.MIN or LocalDate.MIN.plusDays(1)

public static parse(text: string): LocalDateRange source

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

Params:

NameTypeAttributeDescription
text string

the text to parse, not null

Return:

LocalDateRange

the parsed range, not null

Throw:

DateTimeParseException

if the text cannot be parsed

Static Protected Methods

protected static _ofLocalDateLocalDate(startInclusive: LocalDate, endExclusive: LocalDate): LocalDateRange source

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.

Params:

NameTypeAttributeDescription
startInclusive LocalDate

the inclusive start date, not null

endExclusive LocalDate

the exclusive end date, not null

Return:

LocalDateRange

the half-open range, not null

Throw:

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)

protected static _ofLocalDatePeriod(startInclusive: LocalDate, period: Period): LocalDateRange source

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.

Params:

NameTypeAttributeDescription
startInclusive LocalDate

the inclusive start date, not null

period Period

the period from the start to the end, not null

Return:

LocalDateRange

the range, not null

Throw:

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

Public Methods

public abuts(other: LocalDateRange): boolean source

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.

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range, not null

Return:

boolean

true if this range abuts the other range

public contains(date: LocalDate): boolean source

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.

Params:

NameTypeAttributeDescription
date LocalDate

the date to check for, not null

Return:

boolean

true if this range contains the date

public encloses(other: LocalDateRange): boolean source

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.

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range to check for, not null

Return:

boolean

true if this range contains all dates in the other range

public end(): LocalDate source

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

Return:

LocalDate

the end date, exclusive

public endInclusive(): LocalDate source

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.

Return:

LocalDate

the end date, inclusive

public equals(obj: *): boolean source

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.

Params:

NameTypeAttributeDescription
obj *

the object to check, null returns false

Return:

boolean

true if this is equal to the other range

public hashCode(): number source

A hash code for this range.

Return:

number

a suitable hash code

public intersection(other: LocalDateRange): LocalDateRange source

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 {@linkplain #isConnected(LocalDateRange) connected}.

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range to check for, not null

Return:

LocalDateRange

the range that is the intersection of the two ranges

Throw:

DateTimeException

if the ranges do not connect

public isAfter(localDateOrLocalDateRange: LocalDate | LocalDateRange): boolean source

Function overloading for LocalDateRange.isAfter

Params:

NameTypeAttributeDescription
localDateOrLocalDateRange LocalDate | LocalDateRange

Return:

boolean

public isBefore(localDateOrLocalDateRange: LocalDate | LocalDateRange): boolean source

Function overloading for LocalDateRange.isBefore

Params:

NameTypeAttributeDescription
localDateOrLocalDateRange LocalDate | LocalDateRange

Return:

boolean

public isConnected(other: LocalDateRange): boolean source

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

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range, not null

Return:

boolean

true if this range is connected to the other range

public isEmpty(): boolean source

Checks if the range is empty.

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

An empty range is never unbounded.

Return:

boolean

true if the range is empty

public isUnboundedEnd(): boolean source

Checks if the end of the range is unbounded.

An unbounded range is never empty.

Return:

boolean

true if end is unbounded

public isUnboundedStart(): boolean source

Checks if the start of the range is unbounded.

An unbounded range is never empty.

Return:

boolean

true if start is unbounded

public lengthInDays(): number source

Obtains the length of this range in days.

This returns the number of days between the start and end dates. Unbounded ranges return Number.POSITIVE_INFINITY.

Return:

number

the length in days, Number.POSITIVE_INFINITY if unbounded

public overlaps(other: LocalDateRange): boolean source

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

Params:

NameTypeAttributeDescription
other LocalDateRange

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

Return:

boolean

true if the time ranges overlap

public span(other: LocalDateRange): LocalDateRange source

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

The result of this method will {@linkplain #encloses(LocalDateRange) enclose} this range and the specified range.

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range to check for, not null

Return:

LocalDateRange

the range that spans the two ranges

public start(): LocalDate source

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

Return:

LocalDate

the start date

public toPeriod(): Period source

Obtains the length of this range as a period.

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

Return:

Period

the period of the range

Throw:

ArithmeticException

if the calculation exceeds the capacity of Period, or the range is unbounded

public toString(): string source

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.

Return:

string

a string representation of this date, not null

public union(other: LocalDateRange): LocalDateRange source

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 {@linkplain #isConnected(LocalDateRange) connected}.

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range to check for, not null

Return:

LocalDateRange

the range that is the union of the two ranges

Throw:

DateTimeException

if the ranges do not connect

public withEnd(adjuster: TemporalAdjuster): LocalDateRange source

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

Params:

NameTypeAttributeDescription
adjuster TemporalAdjuster

the adjuster to use, not null

Return:

LocalDateRange

a copy of this range with the end date adjusted

Throw:

DateTimeException

if the new end date is before the current start date

public withStart(adjuster: TemporalAdjuster): LocalDateRange source

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

Params:

NameTypeAttributeDescription
adjuster TemporalAdjuster

the adjuster to use, not null

Return:

LocalDateRange

a copy of this range with the start date adjusted

Throw:

DateTimeException

if the new start date is after the current end date

Protected Methods

protected _isAfterLocalDate(date: LocalDate): boolean source

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.

Params:

NameTypeAttributeDescription
date LocalDate

the other date to compare to, not null

Return:

boolean

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

protected _isAfterLocalDateRange(other: LocalDateRange): boolean source

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.

Params:

NameTypeAttributeDescription
other LocalDateRange

the other range to compare to, not null

Return:

boolean

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

protected _isBeforeLocalDate(date: LocalDate): boolean source

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.

Params:

NameTypeAttributeDescription
date LocalDate

the other date to compare to, not null

Return:

boolean

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

protected _isBeforeLocalDateRange(range: LocalDateRange): boolean source

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.

Params:

NameTypeAttributeDescription
range LocalDateRange

the other range to compare to, not null

Return:

boolean

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