Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDate interval in Joda-time

Tags:

java

jodatime

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?

like image 525
Wouter Coekaerts Avatar asked Dec 08 '10 12:12

Wouter Coekaerts


People also ask

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

What is Joda LocalDate?

LocalDate is an immutable datetime class representing a date without a time zone. LocalDate implements the ReadablePartial interface. To do this, the interface methods focus on the key fields - Year, MonthOfYear and DayOfMonth. However, all date fields may in fact be queried.

Is Joda-time format followed in Java 8?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

Does LocalDate contain time?

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03 . LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day.


Video Answer


1 Answers

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.

like image 77
olivr Avatar answered Oct 04 '22 14:10

olivr