public final class DateInterval extends IsoInterval<PlainDate,DateInterval> implements Serializable
Defines a date interval.
| Modifier and Type | Field and Description |
|---|---|
static DateInterval |
ALWAYS
Constant for a date interval from infinite past to infinite future.
|
| Modifier and Type | Method and Description |
|---|---|
static DateInterval |
atomic(LocalDate date)
Creates a closed interval including only given date.
|
static DateInterval |
atomic(PlainDate date)
Creates a closed interval including only given date.
|
static DateInterval |
between(LocalDate start,
LocalDate end)
Creates a closed interval between given dates.
|
static DateInterval |
between(PlainDate start,
PlainDate end)
Creates a closed interval between given dates.
|
static Comparator<ChronoInterval<PlainDate>> |
comparator()
Defines a comparator which sorts intervals first
by start boundary and then by length.
|
static DateInterval |
emptyWithAnchor(LocalDate anchor)
Creates an empty interval with an anchor date.
|
static DateInterval |
emptyWithAnchor(PlainDate anchor)
Creates an empty interval with an anchor date.
|
String |
formatISO(IsoDateStyle dateStyle,
InfinityStyle infinityStyle)
Prints the canonical form of this interval in given ISO-8601 style.
|
String |
formatReduced(IsoDateStyle dateStyle,
InfinityStyle infinityStyle)
Prints the canonical form of this interval in given reduced ISO-8601 style.
|
static DateInterval |
from(ChronoInterval<PlainDate> interval)
Converts an arbitrary date interval to an interval of this type.
|
Duration<CalendarUnit> |
getDuration(CalendarUnit... units)
Yields the length of this interval in given calendrical units.
|
Duration<CalendarUnit> |
getDurationInYearsMonthsDays()
Yields the length of this interval in years, months and days.
|
PlainDate |
getEndAsCalendarDate()
Yields the end date.
|
LocalDate |
getEndAsLocalDate()
Yields the end date.
|
long |
getLengthInDays()
Yields the length of this interval in days.
|
PlainDate |
getStartAsCalendarDate()
Yields the start date.
|
LocalDate |
getStartAsLocalDate()
Yields the start date.
|
MomentInterval |
inTimezone(TZID tzid)
Converts this instance to a moment interval with date boundaries mapped
to the midnight cycle in given time zone.
|
DateInterval |
move(long amount,
IsoDateUnit unit)
Moves this interval along the time axis by given units.
|
static DateInterval |
ofCurrentWeek(TimeSource clock,
TZID tzid,
Weekday firstDay)
Obtains the current calendar week based on given clock, time zone and first day of week.
|
static DateInterval |
parse(CharSequence text,
ChronoParser<PlainDate> parser,
BracketPolicy policy)
Interpretes given text as interval.
|
static DateInterval |
parse(CharSequence text,
ChronoParser<PlainDate> startFormat,
char separator,
ChronoParser<PlainDate> endFormat,
BracketPolicy policy,
ParseLog status)
Interpretes given text as interval.
|
static DateInterval |
parse(String text,
ChronoParser<PlainDate> parser)
Interpretes given text as interval using a localized interval pattern.
|
static DateInterval |
parse(String text,
ChronoParser<PlainDate> parser,
String intervalPattern)
Interpretes given text as interval using given interval pattern.
|
static DateInterval |
parseISO(String text)
Interpretes given ISO-conforming text as interval.
|
PlainDate |
random()
Obtains a random date within this interval.
|
static DateInterval |
since(LocalDate start)
Creates an infinite interval since given start date.
|
static DateInterval |
since(PlainDate start)
Creates an infinite interval since given start date.
|
Stream<PlainDate> |
stream(Duration<CalendarUnit> duration)
Obtains a stream iterating over every calendar date which is the result of addition of given duration
to start until the end of this interval is reached.
|
static Stream<PlainDate> |
stream(Duration<CalendarUnit> duration,
PlainDate start,
PlainDate end)
Obtains a stream iterating over every calendar date which is the result of addition of given duration
to start until the end is reached.
|
Stream<PlainDate> |
streamDaily()
Obtains a stream iterating over every calendar date of the canonical form of this interval.
|
static Stream<PlainDate> |
streamDaily(PlainDate start,
PlainDate end)
Obtains a stream iterating over every calendar date between given interval boundaries.
|
Stream<PlainDate> |
streamExcluding(Predicate<? super PlainDate> exclusion)
Obtains a stream iterating over every calendar date of the canonical form of this interval
and applies given exclusion filter.
|
Stream<TimestampInterval> |
streamPartitioned(DayPartitionRule rule)
Creates a partitioning stream of timestamp intervals where every day of this interval is partitioned
according to given partitioning rule.
|
Stream<MomentInterval> |
streamPartitioned(DayPartitionRule rule,
TZID tzid)
Creates a partitioning stream of moment intervals where every day of this interval is partitioned
according to given partitioning rule.
|
Stream<PlainDate> |
streamWeekBased(int weekBasedYears,
int isoWeeks,
int days)
Obtains a stream iterating over every calendar date which is the result of addition of given duration
in week-based units to start until the end of this interval is reached.
|
TimestampInterval |
toFullDays()
Converts this instance to a timestamp interval with
dates from midnight to midnight.
|
static DateInterval |
until(LocalDate end)
Creates an infinite interval until given end date.
|
static DateInterval |
until(PlainDate end)
Creates an infinite interval until given end date.
|
abuts, collapse, contains, contains, enclosedBy, encloses, equals, equivalentTo, findIntersection, finishedBy, finishes, get, getEnd, getStart, hashCode, intersects, isAfter, isAfter, isBefore, isBefore, isEmpty, meets, metBy, overlappedBy, overlaps, precededBy, precedes, print, print, print, print, startedBy, starts, toCanonical, toString, withClosedEnd, withClosedStart, withEnd, withEnd, withOpenEnd, withOpenStart, withStart, withStart, withValueisFinitepublic static final DateInterval ALWAYS
public static Comparator<ChronoInterval<PlainDate>> comparator()
Defines a comparator which sorts intervals first by start boundary and then by length.
public static DateInterval between(PlainDate start, PlainDate end)
Creates a closed interval between given dates.
start - date of lower boundary (inclusive)end - date of upper boundary (inclusive)IllegalArgumentException - if start is after endpublic static DateInterval between(LocalDate start, LocalDate end)
Creates a closed interval between given dates.
start - date of lower boundary (inclusive)end - date of upper boundary (inclusive)IllegalArgumentException - if start is after endbetween(PlainDate, PlainDate)public static DateInterval since(PlainDate start)
Creates an infinite interval since given start date.
start - date of lower boundary (inclusive)public static DateInterval since(LocalDate start)
Creates an infinite interval since given start date.
start - date of lower boundary (inclusive)since(PlainDate)public static DateInterval until(PlainDate end)
Creates an infinite interval until given end date.
end - date of upper boundary (inclusive)public static DateInterval until(LocalDate end)
Creates an infinite interval until given end date.
end - date of upper boundary (inclusive)until(PlainDate)public static DateInterval atomic(PlainDate date)
Creates a closed interval including only given date.
date - single contained datepublic static DateInterval atomic(LocalDate date)
Creates a closed interval including only given date.
date - single contained dateatomic(PlainDate)public static DateInterval emptyWithAnchor(PlainDate anchor)
Creates an empty interval with an anchor date.
An empty interval does not contain any date but the anchor can modifiy the behaviour
of methods like IsoInterval.meets(IsoInterval). The lower interval boundary is closed,
and the upper interval boundary is open.
anchor - the anchor datepublic static DateInterval emptyWithAnchor(LocalDate anchor)
Creates an empty interval with an anchor date.
anchor - the anchor dateemptyWithAnchor(PlainDate)public static DateInterval ofCurrentWeek(TimeSource clock, TZID tzid, Weekday firstDay)
Obtains the current calendar week based on given clock, time zone and first day of week.
A localized first day of week can be obtained by the expression
Weekmodel.of(Locale.getDefault()).getFirstDayOfWeek(). The
next week can be found by applying move(1, CalendarUnit.DAYS)
on the result of this method. If the first day of week is Monday then
users should consider the alternative ISO calendar week.
clock - the clock for evaluating the current calendar weektzid - time zone in which the calendar week is validfirstDay - first day of weekDateIntervalIllegalArgumentException - if given timezone cannot be loadedSystemClock.INSTANCE,
Timezone.getID(),
Weekmodel.getFirstDayOfWeek(),
move(long, IsoDateUnit)public static DateInterval from(ChronoInterval<PlainDate> interval)
Converts an arbitrary date interval to an interval of this type.
interval - any kind of date intervalpublic PlainDate getStartAsCalendarDate()
Yields the start date.
null if infiniteBoundary.isInfinite()public LocalDate getStartAsLocalDate()
Yields the start date.
null if infiniteBoundary.isInfinite()public PlainDate getEndAsCalendarDate()
Yields the end date.
null if infiniteBoundary.isInfinite()public LocalDate getEndAsLocalDate()
Yields the end date.
null if infiniteBoundary.isInfinite()public TimestampInterval toFullDays()
Converts this instance to a timestamp interval with dates from midnight to midnight.
The resulting interval is half-open if this interval is finite.
public MomentInterval inTimezone(TZID tzid)
Converts this instance to a moment interval with date boundaries mapped to the midnight cycle in given time zone.
The resulting interval is half-open if this interval is finite. Note that sometimes the moments of result intervals can deviate from midnight if midnight does not exist due to daylight saving effects.
tzid - timezone identifierGapResolver.NEXT_VALID_TIME,
OverlapResolver.EARLIER_OFFSETpublic long getLengthInDays()
Yields the length of this interval in days.
UnsupportedOperationException - if this interval is infinitegetDurationInYearsMonthsDays(),
getDuration(CalendarUnit...)public Duration<CalendarUnit> getDurationInYearsMonthsDays()
Yields the length of this interval in years, months and days.
UnsupportedOperationException - if this interval is infinitegetLengthInDays(),
getDuration(CalendarUnit...)public Duration<CalendarUnit> getDuration(CalendarUnit... units)
Yields the length of this interval in given calendrical units.
units - calendrical units as calculation baseUnsupportedOperationException - if this interval is infinitegetLengthInDays(),
getDurationInYearsMonthsDays()public DateInterval move(long amount, IsoDateUnit unit)
Moves this interval along the time axis by given units.
amount - amount of unitsunit - time unit for movingpublic Stream<PlainDate> streamDaily()
Obtains a stream iterating over every calendar date of the canonical form of this interval.
IllegalStateException - if this interval is infinite or if there is no canonical formIsoInterval.toCanonical(),
streamDaily(PlainDate, PlainDate)public static Stream<PlainDate> streamDaily(PlainDate start, PlainDate end)
Obtains a stream iterating over every calendar date between given interval boundaries.
This static method avoids the costs of constructing an instance of DateInterval.
start - start boundary - inclusiveend - end boundary - inclusiveIllegalArgumentException - if start is after endstreamDaily()public Stream<PlainDate> stream(Duration<CalendarUnit> duration)
Obtains a stream iterating over every calendar date which is the result of addition of given duration to start until the end of this interval is reached.
duration - duration which has to be added to the start multiple timesIllegalArgumentException - if the duration is not positiveIllegalStateException - if this interval is infinite or if there is no canonical formIsoInterval.toCanonical(),
stream(Duration, PlainDate, PlainDate)public static Stream<PlainDate> stream(Duration<CalendarUnit> duration, PlainDate start, PlainDate end)
Obtains a stream iterating over every calendar date which is the result of addition of given duration to start until the end is reached.
This static method avoids the costs of constructing an instance of DateInterval.
duration - duration which has to be added to the start multiple timesstart - start boundary - inclusiveend - end boundary - inclusiveIllegalArgumentException - if start is after end or if the duration is not positivepublic Stream<PlainDate> streamExcluding(Predicate<? super PlainDate> exclusion)
Obtains a stream iterating over every calendar date of the canonical form of this interval and applies given exclusion filter.
Example of exclusion of Saturday and Sunday:
DateInterval.between(
PlainDate.of(2017, 2, 1),
PlainDate.of(2017, 2, 8)
).streamExcluding(Weekday.SATURDAY.or(Weekday.SUNDAY)).forEach(System.out::println);
All objects whose type is a subclass of ChronoCondition can be also used as parameter, for example
localized weekends by the expression Weekmodel.of(locale).weekend().
exclusion - filter as predicateIllegalStateException - if this interval is infinite or if there is no canonical formIsoInterval.toCanonical(),
ChronoConditionpublic Stream<PlainDate> streamWeekBased(int weekBasedYears, int isoWeeks, int days)
Obtains a stream iterating over every calendar date which is the result of addition of given duration in week-based units to start until the end of this interval is reached.
weekBasedYears - duration component of week-based yearsisoWeeks - duration component of calendar weeks (from Monday to Sunday)days - duration component of ordinary calendar daysIllegalStateException - if this interval is infinite or if there is no canonical formIllegalArgumentException - if there is any negative duration component or if there is
no positive duration component at allIsoInterval.toCanonical(),
Weekcycle.YEARSpublic Stream<TimestampInterval> streamPartitioned(DayPartitionRule rule)
Creates a partitioning stream of timestamp intervals where every day of this interval is partitioned according to given partitioning rule.
This method enables the easy construction of daily shop opening times or weekly work time schedules.
rule - day partition ruleIllegalStateException - if this interval is infinite or if there is no canonical formstreamPartitioned(DayPartitionRule, TZID)public Stream<MomentInterval> streamPartitioned(DayPartitionRule rule, TZID tzid)
Creates a partitioning stream of moment intervals where every day of this interval is partitioned according to given partitioning rule.
This method enables the easy construction of daily shop opening times or weekly work time schedules.
rule - day partition ruletzid - timezone identifierIllegalStateException - if this interval is infinite or if there is no canonical formIllegalArgumentException - if given timezone cannot be loadedstreamPartitioned(DayPartitionRule),
GapResolver.NEXT_VALID_TIME,
OverlapResolver.EARLIER_OFFSETpublic PlainDate random()
IllegalStateException - if this interval is infinite or empty or if there is no canonical formIsoInterval.toCanonical()public String formatISO(IsoDateStyle dateStyle, InfinityStyle infinityStyle)
Prints the canonical form of this interval in given ISO-8601 style.
dateStyle - controlling the date format of outputinfinityStyle - controlling the format of infinite boundariesIllegalStateException - if there is no canonical form
or given infinity style prevents infinite intervalsIsoInterval.toCanonical()public String formatReduced(IsoDateStyle dateStyle, InfinityStyle infinityStyle)
Prints the canonical form of this interval in given reduced ISO-8601 style.
The term "reduced" means that higher-order elements like the year can be left out in the end component if it is equal to the value of the start component. Example:
DateInterval interval =
DateInterval.between(
PlainDate.of(2016, 2, 29),
PlainDate.of(2016, 3, 13));
System.out.println(interval.formatReduced(IsoDateStyle.EXTENDED_CALENDAR_DATE));
// Output: 2016-02-29/03-13
dateStyle - controlling the date format of outputinfinityStyle - controlling the format of infinite boundariesIllegalStateException - if there is no canonical form
or given infinity style prevents infinite intervalsIsoInterval.toCanonical()public static DateInterval parse(String text, ChronoParser<PlainDate> parser) throws ParseException
Interpretes given text as interval using a localized interval pattern.
If given parser does not contain a reference to a locale then the interval pattern "{0}/{1}" will be used.
text - text to be parsedparser - format object for parsing start and end componentsIndexOutOfBoundsException - if given text is emptyParseException - if the text is not parseableparse(String, ChronoParser, String),
FormatPatternProvider.getIntervalPattern(Locale)public static DateInterval parse(String text, ChronoParser<PlainDate> parser, String intervalPattern) throws ParseException
Interpretes given text as interval using given interval pattern.
Starting with version v4.18, it is also possible to use an or-pattern logic. Example:
String multiPattern = "{0} - {1}|since {0}|until {1}";
ChronoParser<PlainDate> parser =
ChronoFormatter.ofDatePattern("MMMM d / uuuu", PatternType.CLDR, Locale.US);
DateInterval between =
DateInterval.parse("July 20 / 2015 - December 31 / 2015", parser, multiPattern);
System.out.println(between); // [2015-07-20/2015-12-31]
DateInterval since =
DateInterval.parse("since July 20 / 2015", parser, multiPattern);
System.out.println(since); // [2015-07-20/+∞)
DateInterval until =
DateInterval.parse("until December 31 / 2015", parser, multiPattern);
System.out.println(until); // (-∞/2015-12-31]
text - text to be parsedparser - format object for parsing start and end componentsintervalPattern - interval pattern containing placeholders {0} and {1} (for start and end)IndexOutOfBoundsException - if given text is emptyParseException - if the text is not parseablepublic static DateInterval parse(CharSequence text, ChronoParser<PlainDate> parser, BracketPolicy policy) throws ParseException
Interpretes given text as interval.
This method can also accept a hyphen as alternative to solidus as separator between start and end component unless the start component is a period. Infinity symbols are understood.
text - text to be parsedparser - format object for parsing start and end componentspolicy - strategy for parsing interval boundariesParseException - if parsing does not workIndexOutOfBoundsException - if the start position is at end of text or even behindpublic static DateInterval parse(CharSequence text, ChronoParser<PlainDate> startFormat, char separator, ChronoParser<PlainDate> endFormat, BracketPolicy policy, ParseLog status)
Interpretes given text as interval.
This method is mainly intended for parsing technical interval formats similar to ISO-8601 which are not localized. Infinity symbols are understood.
text - text to be parsedstartFormat - format object for parsing start componentseparator - char separating start and end componentendFormat - format object for parsing end componentpolicy - strategy for parsing interval boundariesstatus - parser information (always as new instance)null if parsing does not workIndexOutOfBoundsException - if the start position is at end of text or even behindpublic static DateInterval parseISO(String text) throws ParseException
Interpretes given ISO-conforming text as interval.
All styles are supported, namely calendar dates, ordinal dates and week dates, either in basic or in extended format. Mixed date styles for start and end are not allowed however. Furthermore, one of start or end can also be represented by a period string. If not then the end component may exist in an abbreviated form as documented in ISO-8601-paper leaving out higher-order elements like the calendar year (which will be overtaken from the start component instead).
The infinity symbols "-" (past and future), "-∞" (past), "+∞" (future), "-999999999-01-01" und "+999999999-12-31" can also be parsed as extension although strictly spoken ISO-8601 does not know or specify infinite intervals. Examples for supported formats:
System.out.println(
DateInterval.parseISO("2012-01-01/2014-06-20"));
// output: [2012-01-01/2014-06-20]
System.out.println(DateInterval.parseISO("2012-01-01/08-11"));
// output: [2012-01-01/2012-08-11]
System.out.println(DateInterval.parseISO("2012-W01-1/W06-4"));
// output: [2012-01-02/2012-02-09]
System.out.println(DateInterval.parseISO("2012-001/366"));
// output: [2012-01-01/2012-12-31]
System.out.println(DateInterval.parseISO("2012-001/+∞"));
// output: [2012-01-01/+∞)
This method dynamically creates an appropriate interval format for reduced forms. If performance is more important then a static fixed formatter might be considered.
text - text to be parsedIndexOutOfBoundsException - if given text is emptyParseException - if the text is not parseableBracketPolicy.SHOW_NEVERCopyright © 2014–2021. All rights reserved.