Interval

An interval in Joda-Time represents an interval of time from one millisecond instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone.

Intervals are implemented as half-open, which is to say that the start instant is inclusive but the end instant is exclusive. The end is always greater than or equal to the start. The interval is also restricted to just one chronology and time zone.

Methods exist on intervals to obtain the start and end instants plus the chronology and time zone. There are also methods to obtain the duration and period of the interval.

You cannot compare intervals for order (ie. they do not implement Comparable. If you want to compare the length of intervals you must obtain the duration of each and compare those.

Using Intervals in Joda-Time

Within Joda-Time an interval is represented by the ReadableInterval interface. There are two implementations of the interface provided:

We recommend the immutable implementation for general usage.

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);
Accessing other objects is easy:
Interval interval = ...
DateTime start = interval.getStart();
DateTime end = interval.getEnd();
Chronology chrono = interval.getChronology();
Duration duration = interval.toDuration();
Period period = interval.toPeriod();

Note that the interface ReadableInterval should not be used like the collections API. The interface only contains the core subset of the operations. Instead, you should usually refer directly to the implementation classes in your APIs.

Nulls

Joda-Time defines a null interval as a zero length interval from now to now. Thus, when a method is defined as taking a ReadableInterval, passing null in will be the same as passing in a zero length interval at the current time.

Back to top

Version: 2.6. Last Published: 2014-12-02.

Reflow Maven skin by Andrius Velykis.