Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime within interval in JodaTime?

Tags:

java

jodatime

Is there a API method in JodaTime to see whether a DateTime is within [start, end], i.e. the boundary is included?

Only thing I found was new Interval(start, end).contains(dateTime) but this seems to give false if dateTime equals end.

like image 562
egaga Avatar asked May 22 '09 05:05

egaga


People also ask

How to create Time interval in java?

The code can be used in various ways: // interval from start to end DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0); DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0); Interval interval = new Interval(start, end);

Why use Joda-Time?

Joda-Time provides support for multiple calendar systems and the full range of time-zones. The Chronology and DateTimeZone classes provide this support. Joda-Time defaults to using the ISO calendar system, which is the de facto civil calendar used by the world.

What is Joda-Time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.

What is interval in Java?

Interval is the standard implementation of an immutable time interval. A time interval represents a period of time between two instants. Intervals are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.


1 Answers

The easiest approach would be to create a new interval which is the old one extended by a millisecond if you need to do this often:

Interval inclusiveInterval = interval.withEndMillis(interval.getEndMillis() + 1);

Alternatively just use

if (interval.contains(dateTime) || interval.getEnd().isEqual(dateTime))

(The isEqual method ignores chronology and time zone.)

like image 159
Jon Skeet Avatar answered Oct 16 '22 03:10

Jon Skeet