ISO 8601 defines a syntax for representing a time interval.
There are four ways to express a time interval:
- Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
- Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
- Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z"
- Duration only, such as "P1Y2M10DT2H30M", with additional context information
If any elements are missing from the end value, they are assumed to be the same as for the start value including the time zone. This feature of the standard allows for concise representations of time intervals. For example, the date of a two-hour meeting including the start and finish times could be simply shown as "2007-12-14T13:30/15:30", where "/15:30" implies "/2007-12-14T15:30" (the same date as the start), or the beginning and end dates of a monthly billing period as "2008-02-15/03-14", where "/03-14" implies "/2008-03-14" (the same year as the start).
In addition, repeating intervals are formed by adding "R[n]/" to the beginning of an interval expression, where R is used as the letter itself and [n] is replaced by the number of repetitions. Leaving out the value for [n] means an unbounded number of repetitions. So, to repeat the interval of "P1Y2M10DT2H30M" five times starting at "2008-03-01T13:00:00Z", use "R5/2008-03-01T13:00:00Z/P1Y2M10DT2H30M".
I am looking for a good Java parser (if possible compatible with the Joda-Time library) to parse this syntax. Any pointers to a good library ?
For anyone on a project that might be restricted from using 3rd party libraries (licensing reasons, or whatever), Java itself provides at least a portion of this capability, since Java 1.6 (or earlier?), using the javax.xml.datatype.DatatypeFactory.newDuration(String) method and Duration class. The DatatypeFactory.newDuration(String) method will parse a string in "PnYnMnDTnHnMnS" format. These classes are intended for XML manipulation, but since XML uses ISO 8601 time notation, they also serve as convenient duration parsing utilities.
Example:
I haven't personally used any duration format except the 4th one you list, so I can't vouch for whether it successfully parses them or not.
I take it you have already tried Joda-Time? Feeding the example strings from your question through
Interval.parse(Object)
reveals that it can handle "start and end", "start and duration" and "duration and end", but not implied fields nor repetition.The only other comprehensive date/time library that I know of is JSR-310, which does not appear to handle intervals like these.
At this point, building your own improvements on top of Joda-Time is probably your best choice, sorry. Are there any specific ISO interval formats that you need to handle beyond those already supported by Joda-Time?
java.time
The java.time framework built into Java 8 and later has a
Duration.parse
method for parsing an ISO 8601 formatted duration:Prints
Duration in seconds: 3754
The only library which is capable to model all the features of interval parsing you want is actually my library Time4J (range-module). Examples:
Some remarks:
Other interval classes exist with similar parsing capabilities, for example
DateInterval
orTimestampInterval
in the package net.time4j.range.For handling durations only (which can span both calendar and clock units as well), see also the javadoc. There are also formatting features, see nested class
Duration.Formatter
or the localized versionnet.time4j.PrettyTime
(actually in 86 languages).Interoperability is offered with Java-8 (
java.time
-package) but not with Joda-Time. For example: The start or end component of aMomentInterval
can easily be queried bygetStartAsInstant()
orgetEndAsInstant()
.Repeating intervals are supported by the class IsoRecurrence. Example:
Output: