I want to define a date range like this:
- start from
"1396/8/9"
- end to
"1396/9/2"
And get the days date between these two dates.
I searched through the Internet and I found a DateInterval
function in Time4j.
Is it possible to use this method for defining optional date range in Persian calendar? If yes, how can I use this method for my purpose?
Using conversions
The class DateInterval is designed for ISO-8601 only that is for the proleptic gregorian calendar. So you would be responsible yourself for conversions between the gregorian type
PlainDate
and the target typePersianCalendar
:And for the reverse conversion:
Of course, you can also loop daily through your interval:
Side note:
DateInterval
is closed by default (as required in most business date-related applications) but can be configured as half-open by callinginterval.withOpenEnd()
.Alternative:
There is even an approach to store instances of
PersianCalendar
directly in an interval, namely SimpleInterval (attention: always half-open before release v4.31!). Example:Daily looping via an ordinary for-loop:
However, I don't really recommend to use
SimpleInterval
because a) it has less features thanDateInterval
and b) is mainly designed for non-calendrical types likejava.util.Date
(since such intervals are always half-open while calendrical intervals should better be closed in most business applications).Optional ranges
About your term "optional date range", I am not clear about the meaning. Could you clarify? Maybe you are talking about infinite or half-infinite interval boundaries. This is possible. Just construct your intervals not by
between(...)
but bysince(...)
oruntil(...)
. But this has impact on your looping code (you cannot loop from or until infinity.).Update (2017-10-21)
Starting with release v4.31, the class
SimpleInterval
has been enhanced such that it can process calendrical types in a better way, namely always closed instead of half-open (but instant-like intervals will still be half-open, of course). Some new methods were introduced for this purpose, for example on(TimeAxis). The javadoc behind the link shows an example:Such a calendar interval can be processed within
IntervalCollection
orIntervalTree
as usual.