32

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?

Wouter Coekaerts
  • 9,395
  • 3
  • 31
  • 35

6 Answers6

29

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.

overthink
  • 23,985
  • 4
  • 69
  • 69
olivr
  • 346
  • 3
  • 7
15

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.

Olivier Heidemann
  • 1,337
  • 10
  • 14
7

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • But what I need is not the duration, but a specific interval. For example: from 1/1/2011 to 10/1/2012. I'll edit the question to clarify – Wouter Coekaerts Dec 08 '10 at 12:57
  • Doh, sorry, I'm with you now. – Jon Skeet Dec 08 '10 at 12:57
  • you shouldn't remove it. at least the java version of the interval class is often used – Olivier Heidemann Dec 08 '10 at 14:05
  • @Olivier: Really? I personally haven't used it, nor can I see it being terribly useful... I guess it depends on your use case though. – Jon Skeet Dec 08 '10 at 15:23
  • @Jon for example in case of an time fixed task with an immutable duration. I often use an interval. This would be backed by start and end instant fields in the db. Or alternatively an date and two "minutes after midnight" fields – Olivier Heidemann Dec 08 '10 at 16:22
4

Note that using DateMidnight will fail at the DST switch. It is better to convert using date.toDateTimeAtStartOfDay().

Nicholas Pappas
  • 10,439
  • 12
  • 54
  • 87
2

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 it open source.

You can check it out: https://github.com/serddmitry/joda-interval (available on Maven Central).

Pang
  • 9,564
  • 146
  • 81
  • 122
Dmitry Serdiuk
  • 468
  • 3
  • 17
1
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.

Stimpson Cat
  • 1,444
  • 19
  • 44