Class DateRange
- All Implemented Interfaces:
Serializable,Comparable<DateRange>
Date-based range, used as
the value object of Daterangebox.
Either end may be null to denote an open-ended range:
begin == null && end == null— empty range.begin == null && end != null— open start ("beforeend").begin != null && end == null— open end ("afterbegin").- both non-null — closed range.
The class intentionally does not auto-swap when
begin > end; the raw input is preserved so the UI layer can decide
how to surface the inversion.
Instances are Serializable and Comparable: ordering compares
begin first then end, treating null as the smallest value.
- Since:
- 11.0.0
- Author:
- peaker
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintbooleanTests whether the supplied date falls within this range.booleangetBegin()Returns the begin date, ornullif the range has an open start.getDays()Returns the inclusive 24-hour-block count betweenbeginandend.getEnd()Returns the end date, ornullif the range has an open end.inthashCode()booleanisClosed()booleanisEmpty()booleanbooleanstatic DateRangeStatic factory equivalent toDateRange(Date, Date).booleanTests whether this range overlaps the supplied range.toString()Returns a debug representation in the form"yyyy-MM-dd ~ yyyy-MM-dd".
-
Constructor Details
-
DateRange
Constructs a new range. Eitherbeginorendmay benullto denote an open-ended range.- Parameters:
begin- the begin date (inclusive), ornullfor open startend- the end date (inclusive), ornullfor open end
-
-
Method Details
-
of
Static factory equivalent toDateRange(Date, Date).- Parameters:
begin- the begin date (inclusive), ornullend- the end date (inclusive), ornull- Returns:
- a new
DateRange
-
getBegin
Returns the begin date, ornullif the range has an open start.- Returns:
- the begin date (defensive copy), or
null
-
getEnd
Returns the end date, ornullif the range has an open end.- Returns:
- the end date (defensive copy), or
null
-
isEmpty
public boolean isEmpty()- Returns:
trueif both ends arenull.
-
isOpenStart
public boolean isOpenStart()- Returns:
trueifbeginisnullbutendis not.
-
isOpenEnd
public boolean isOpenEnd()- Returns:
trueifendisnullbutbeginis not.
-
isClosed
public boolean isClosed()- Returns:
trueif both ends are non-null.
-
contains
Tests whether the supplied date falls within this range. The comparison is null-safe and inclusive on both ends.nullends are treated as unbounded (negative or positive infinity respectively).- Parameters:
value- the date to test, may benull- Returns:
trueifvalueis within the range
-
getDays
Returns the inclusive 24-hour-block count betweenbeginandend. A range whose endpoints fall on the same UTC instant returns1; each additional full 24 h adds1.Wall-clock, not calendar-day, semantics. The result is derived from
end - beginin milliseconds, divided by 24 h. A range like2026-01-01 23:59 → 2026-01-02 00:01spans two calendar dates but returns1because the wall-clock duration is only two minutes. Likewise a DST "spring forward" range whose endpoints both read02:30local time can return0because the underlying duration is 23 h. For zone-aware calendar-day counting useLocalDateRange.getDays()instead.Returns an empty
OptionalLongwhen the range is not closed (either end isnull). When the range is closed and inverted (endbeforebegin) a non-positive value is wrapped in the optional — this class does not auto-swap. The optional return type is deliberate: it forces callers to acknowledge the "open-ended" case at compile time, replacing the previousLong.MIN_VALUEsentinel which collided with sub-24 h inverted ranges.Inverted-range semantics. The signed formula is
diffMs >= 0 ? toDays(diffMs) + 1 : toDays(diffMs) - 1. The inclusive-count language only applies to forward ranges; for inverted ranges the returned value is a negative "signed inclusive count" (e.g. begin=Jan 5, end=Jan 4 →-2; begin=Jan 5 noon, end=Jan 5 morning →-1). Callers reasoning about magnitude should applyMath.abs(...); callers wanting only forward ranges should swap endpoints before calling, or checkcompareTo(...).- Returns:
- the inclusive 24-h-block count for closed ranges (negative
when inverted), or
OptionalLong.empty()when either endpoint is null
-
overlaps
Tests whether this range overlaps the supplied range.nullends are treated as unbounded. An empty range never overlaps.- Parameters:
other- the other range- Returns:
trueif the two ranges share at least one instant
-
equals
-
hashCode
public int hashCode() -
toString
Returns a debug representation in the form"yyyy-MM-dd ~ yyyy-MM-dd". Theyyyy-MM-ddpattern is locale-insensitive and the zone is UTC, so the same range prints identically across JVMs and locales — this is meant for logs and diagnostics, not user-facing display. For zone-aware formatting use theDateRangevalue together with the consumer's ownSimpleDateFormat/DateTimeFormatter. -
compareTo
- Specified by:
compareToin interfaceComparable<DateRange>
-