Joda-time has an Interval class, which is a range between DateTimes. What can be used for a range of LocalDates?
I want an object that represents, for example \"from
Note that using DateMidnight
will fail at the DST switch. It is better to convert using date.toDateTimeAtStartOfDay()
.
I needed intervals of LocalDate objects too but it's not supported by JodaTime. So I had to create my own implementation. Later I've decided to polish it a bit and make open source.
You can check it out: https://github.com/serddmitry/joda-interval (available on Maven Central).
EDIT: Oops, misread that before.
No, there's nothing equivalent in Joda Time that I'm aware of.
Personally I would just create my own class which included the start and end date: LocalDateInterval
or something like that. I wouldn't reuse Interval
for this, as it would be confusing the concepts: a pair of LocalDate
values doesn't represent two instants, as a local date isn't really an instant.
Personally I don't find Interval
particularly useful in Joda Time... I'd been considering removing it from Noda Time. Now it sounds like I shouldn't ;)
I personnaly use the Range class from Guava.
It supports open ended ranges. It is also possible to specify included or excluded bounds. Among other numerous possibilities, those allow to easily represent "before a date" or "after a date".
Example for open-ended intervals.
Range<LocalDate> before2010 = Range.atMost(new LocalDate("2009-12-31"));
Range<LocalDate> alsoBefore2010 = Range.lessThan(new LocalDate("2010-01-01"));
It also offerts easy testing predicates, like contains and containsAll, and an intersection operation. All this tested and maintained.
public class LocalDateInterval {
private LocalDate begin;
private LocalDate end;
public LocalDateInterval(LocalDate begin, LocalDate end) {
this.begin = begin;
this.end = end;
}
}
This works for me.
The Interval class represents an interval of time from one millisecond instant to another instant (complete with timezone). DateMidnight is such an instant. So your proposed implementation of an interval for DateMidnights is allready there.
Interval i = new Interval(
new DateMidnight(2010, 3, 2), new DateMidnight(2010, 3, 5));
i.contains(new DateTime(2010, 3, 1, 23, 59, 59, 999)); // == false
i.contains(new DateTime(2010, 3, 2, 0, 0, 0, 0)); // == true
i.contains(new DateTime(2010, 3, 4, 23, 59, 59, 999)); // == true
i.contains(new DateTime(2010, 3, 5, 0, 0, 0, 0)); // == false
However the concept of your searched interval for LocalDates aka a Partial (without timezones) is not yet existent. If you're going to implement it for yourself, don't implement any interval interfaces at all. It would interfere with your wishes, because it is, as stated before, based on instants.