0

Is it possible to make a range object similar to the Joda Time's Interval class where you can set a start and end date value but without including the year? For example, I would I like to do something in my code if a date is within the range August 1st and December 1st, regardless of the year.

David Velasquez
  • 2,346
  • 1
  • 26
  • 46
  • 1
    Of course it's possible. This should be fairly easy once you decide what the API looks like. Have you tried anything? What exactly is your question? – Jim Garrison Jul 29 '16 at 04:50
  • @JimGarrison Yeah I've tried using the LocalDate class and the Interval class but they require a year parameter. And I'm going to be checking if a given local date is within that range, but if a year value is set, it may not work, because the years may be different. – David Velasquez Jul 29 '16 at 04:54
  • @JimGarrison I'm asking how this can be done in Joda Time. Is there a class or some function that will allow me to set a range without the year being involved. I can't find anything on SO or in its documentation addressing this problem. – David Velasquez Jul 29 '16 at 04:57
  • You have to decide what you mean when a range includes the boundary between February and March. Is it 28 or 29 days? – Jim Garrison Jul 29 '16 at 04:59

1 Answers1

2

Joda-Time provides a MonthDay type that implements ReadablePartial (see the list of implementing classes for more types of partial times). You can combine this with Guava's Range type to represent a range of such intervals.

Joda-Time does not include a robust way to work with ranges of partials; your options for time-spans are Period, Interval, and Duration. I've found Range to be very suitable for such tasks, however.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • I just tested this and it works when I test a range that does not overlap into a new year. However I need to be able to test for a range from September through the end of January and when I run the test it crashes where it initializes January 31st. Like this: `Range mRegSeason = Range.closed(new MonthDay(9, 1), new MonthDay(1, 31));` Is there a work around or am I going to have to account for this with boilerplate code? – David Velasquez Jul 29 '16 at 05:56
  • Well yes, January comes before September. `MonthDay` is an ordered type representing just months and days. If you want to incorporate years you'll need to use `LocalDate` or another date type. – dimo414 Jul 29 '16 at 06:10