75

I'm evaluating to migrate my project from the usage of Joda-Time to the java.time package in Java 8. In Joda-Time, I heavily used the Interval class. I could not find anything like this in java.time.

Is there a comparable class?

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
dajood
  • 3,758
  • 9
  • 46
  • 68

4 Answers4

56

Sorry for you, there is no equivalent in JSR-310 to JodaTime-Interval-class. I have doubts if this will ever come, but project lead Stephen Colebourne considers at least to support it in the scope of his external library Threeten-Extra, see this issue.

If you are happy with JodaTime you should keep it. Not everything in JodaTime is ported to Java 8 (Interval is not the only issue).

Update from 2014-12-13:

The situation in Java-8 has not changed, but you might also consider other external libraries beyond Joda-Time. Either Threeten-Extra which now includes a very simple interval class since v0.9 (see the other answer of S. Colebourne here) or my library Time4J which offers the range package since v2.0.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • What are other differences between joda.time and java8.time? Is there a comparison article somewhere? – Alexander Myltsev Jun 26 '14 at 08:32
  • @AlexanderMyltsev There are many! Once I had given a (very short and out-dated) comparison on my blog in German. If I have time I should write a new one ;-). The technical base of JSR-310 is surely more modern (for example using enums) and the internal implementation details are healthier causing a better performance characteristics. On the other side JSR-310 was and will be never intended to be a complete time library (well, Joda-Time is also by far not complete but offers some more features like intervals, period formatters etc.). An exception are localized weekday nums, better in JSR-310. – Meno Hochschild Jun 26 '14 at 09:27
  • 1
    I just stumpled upon this question and I'm curious: what's wrong with the class [Duration](http://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#toHours--)? – wullxz Oct 14 '14 at 01:29
  • 4
    @wullxz The class `Duration` (and `Period`, too) is not anchored on a given timeline, that means has no defined start time, just a length. In contrast, an interval always has a start and an end time. – Meno Hochschild Oct 14 '14 at 01:44
  • Would a simple composite class (called Interval) that wraps a DateTime and a Duration not serve this purpose adequately? It seems to me that all the tools to work with intervals are present. – john16384 Apr 14 '15 at 08:40
  • 1
    @john16384 If you look at my interval implementations mentioned above then you will see that there is much more stuff to explore. For example special interval comparison relationships or specialized formatting capabilities or infinite boundaries or open/closed boundaries or other types like date ranges. Considering all these aspects, the Joda-solution is pretty small, the same valid for Threeten-Extra (rather weak in terms of count of features). Guava range sets and Time4J are much better here. – Meno Hochschild Apr 14 '15 at 09:33
  • @john16384 On the other side you are half-right. The Joda-Interval-class is so simple and limited that it is not difficult to replace it by a hand-made own class with either start-time and duration or start/end-times. Not such a big deal. Just look at the original source code. – Meno Hochschild Apr 14 '15 at 09:59
28

JDK 8 JSR-310 does not have an Interval class. The concept of intervals was descoped to ensure that the rest of the library could be completed.

The ThreeTen-Extra project hosts additional non-JDK date-time classes, and v0.9 includes Interval.

Note: Answer updated 2014-12-10 to include presence of Interval in ThreeTen-Extra.

mkobit
  • 43,979
  • 12
  • 156
  • 150
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
  • As you state 'could be completed', is that for consideration of time, or for other considerations? I think that the introduction of an `Interval` could be confusing, ambiguous, and dangerous. Dangerous because an `Interval` is still within the frame of the exact dates (it has an end-date and begin-date), which is something that could be easily missed? I think that the omission of `Interval` made it a cleaner implementation? `Period` and `Duration` are to me alternatives without the drama. – YoYo Aug 23 '16 at 01:59
  • 1
    'completed' as in finished in time for Java 8 release date. Interval will not be added in Java 9 either. Really the JDK needs a proper range concept. – JodaStephen Aug 23 '16 at 08:43
17

Use Range class from Guava.

See https://stackoverflow.com/a/23485533/1214902 for more details.

Community
  • 1
  • 1
dap.tci
  • 2,455
  • 1
  • 20
  • 18
-5

Class java.time.Duration may give you similar functionality. In general java 8 time package is very comprehensive and flexible

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36