Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ISO 8601 Time Interval Parsing in Java

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 ?

like image 700
fellahst Avatar asked Apr 12 '13 17:04

fellahst


People also ask

What is ISO 8601 date format Java?

The Date/Time API in Java works with the ISO 8601 format by default, which is (yyyy-MM-dd) . All Dates by default follow this format, and all Strings that are converted must follow it if you're using the default formatter.

How do I specify the duration in an ISO 8601 format?

Briefly, the ISO 8601 notation consists of a P character, followed by years, months, weeks, and days, followed by a T character, followed by hours, minutes, and seconds with a decimal part, each with a single-letter suffix that indicates the unit. Any zero components may be omitted.

Does ISO 8601 have milliseconds?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).


2 Answers

java.time

The java.time framework built into Java 8 and later has a Duration.parse method for parsing an ISO 8601 formatted duration:

java.time.Duration d = java.time.Duration.parse("PT1H2M34S"); System.out.println("Duration in seconds: " + d.get(java.time.temporal.ChronoUnit.SECONDS)); 

Prints Duration in seconds: 3754

like image 57
Shaun Parker Avatar answered Sep 20 '22 15:09

Shaun Parker


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:

import javax.xml.datatype.*;  Duration dur = DatatypeFactory.newInstance().newDuration("PT5H12M36S"); int hours = dur.getHours(); // Should return 5 

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.

like image 42
Ogre Psalm33 Avatar answered Sep 18 '22 15:09

Ogre Psalm33