Class DateRange
- java.lang.Object
-
- org.zkoss.zul.DateRange
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<DateRange>
public final class DateRange extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<DateRange>
An immutable value object representing aDate-based range, used as thevalueobject ofDaterangebox.Either end may be
nullto 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
SerializableandComparable: ordering comparesbeginfirst thenend, treatingnullas the smallest value.- Since:
- 10.4.0
- Author:
- peaker
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DateRange(java.util.Date begin, java.util.Date end)Constructs a new range.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(DateRange o)booleancontains(java.util.Date value)Tests whether the supplied date falls within this range.booleanequals(java.lang.Object o)java.util.DategetBegin()Returns the begin date, ornullif the range has an open start.java.util.OptionalLonggetDays()Returns the inclusive 24-hour-block count betweenbeginandend.java.util.DategetEnd()Returns the end date, ornullif the range has an open end.inthashCode()booleanisClosed()booleanisEmpty()booleanisOpenEnd()booleanisOpenStart()static DateRangeof(java.util.Date begin, java.util.Date end)Static factory equivalent toDateRange(Date, Date).booleanoverlaps(DateRange other)Tests whether this range overlaps the supplied range.java.lang.StringtoString()Returns a debug representation in the form"yyyy-MM-dd ~ yyyy-MM-dd".
-
-
-
Method Detail
-
of
public static DateRange of(java.util.Date begin, java.util.Date end)
Static factory equivalent toDateRange(Date, Date).- Parameters:
begin- the begin date (inclusive), ornullend- the end date (inclusive), ornull- Returns:
- a new
DateRange
-
getBegin
public java.util.Date getBegin()
Returns the begin date, ornullif the range has an open start.- Returns:
- the begin date (defensive copy), or
null
-
getEnd
public java.util.Date 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
public boolean contains(java.util.Date value)
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
public java.util.OptionalLong 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
public boolean overlaps(DateRange other)
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
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String 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.- Overrides:
toStringin classjava.lang.Object
-
-