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.
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);
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.
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With