开发者

LocalDate interval in Joda-time

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 1/1/2011 to 10/1/2011", without the time (or timezones) ever coming into the picture.

As far as I see, joda-time doesn't开发者_如何学编程 have anything built in for that. If there doesn't exist anything for it, and we'd create it, what should it look like? Most importantly, which interfaces from joda-time could it implement to best integrate into the other types, staying consistent with the joda-time design? Would it make sense for it to implement ReadableInterval, where getStart and getEnd return DateMidnight?


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.


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.


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 ;)


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜