Parsing ISO-8601 Duration Values ​​for the AMAZON.DURATION Slot

Does the library provide a java.timeconsolidated way to analyze the entire ISO-8601 duration specification ?

The Alexa word type link for the duration contains some sample lines to be expected when using the AMAZON.DURATION slot type. All lines in ISO-8601 Duration, but P2YT3H10cannot be parsed java.time.Periodeither java.time.Duration.

Seq(
 "PT10M",
 "PT5H",
 "P3D",
 "PT45S",
 "P8W",
 "P7Y",
 "PT5H10M",
 "P2YT3H10"
).map { s =>
 s -> Try {
   try {
     Period.parse(s)
   } catch {
     case ex: Throwable => Duration.parse(s)
   }
 }.map(x => x.toString -> x.getClass.getSimpleName)
}
.foreach(println)

Results:

(PT10M,Success((PT10M,Duration)))
(PT5H,Success((PT5H,Duration)))
(P3D,Success((P3D,Period)))
(PT45S,Success((PT45S,Duration)))
(P8W,Success((P56D,Period)))
(P7Y,Success((P7Y,Period)))
(PT5H10M,Success((PT5H10M,Duration)))
(P2YT3H10,Failure(java.time.format.DateTimeParseException: Text cannot be parsed to a Duration))
+6
source share
3 answers

Hugos ISO-8601, "T" -. java.time, Threeten-Extra ( PeriodDuration v1.2). :

ISO-8601 .

"P8W". java.time Threeten-Extra "P56D", . . java.time.Duration , . . :

System.out.println(Duration.parse("PT4200S")); // PT1H10M

, : , , .

:

  • ISO-8601 ( 4.4.4.2) "" ( ), java.time , Period Duration ( ).
  • , , , ISO-8601: "PnnYnnMnnDTnnHnnMnnS" "PnnW".
  • ISO-8601 (. 3.4.2: "n", ), java.time . , XML- , ( "P" ), , .
  • , : "PYYYYMMDDThmmss" . "PYYYY--DDThh: : ".
  • : ISO-8601 ( ), java.time.Duration .

, :

ISO-8601 java.time ( Threeten-Extra, Period java.time.Duration ).

ISO-8601:

, java.time, Time4J, ISO-8601. . API net.time4j.Duration. java.time:

Duration<CalendarUnit> d1 = Duration.parseCalendarPeriod("P8W");
System.out.println(d1); // P8W
System.out.println(d1.getPartialAmount(CalendarUnit.WEEKS)); // 8
System.out.println(Duration.Formatter.ofPattern(CalendarUnit.class, "W' weeks'").format(d1)); // 8 weeks
System.out.println(PrettyTime.of(Locale.GERMAN).print(d1)); // 8 Wochen
LocalDate ld = LocalDate.of(2017, 9, 17);
System.out.println(PlainDate.from(ld).plus(d1)); // 2017-11-12
System.out.println(PlainDate.of(2017, 9, 17).plus(d1)); // 2017-11-12

Duration<IsoUnit> d2 = Duration.parsePeriod("P2DT5H10M");
LocalDateTime ldt = LocalDateTime.of(2017, 9, 17, 19, 15);
System.out.println(PlainTimestamp.from(ldt).plus(d2)); // 2017-09-20T00:25
System.out.println(PlainTimestamp.of(2017, 9, 17, 19, 15).plus(d2)); // 2017-09-20T00:25
System.out.println(PrettyTime.of(Locale.GERMAN).print(d2)); // 2 Tage, 5 Stunden und 10 Minuten

Duration<IsoUnit> d3 = Duration.parsePeriod("P0001-01-02T05:10:04");
System.out.println(d3); // P1Y1M2DT5H10M4S
LocalDateTime ldt = LocalDateTime.of(2017, 9, 17, 19, 15);
System.out.println(PlainTimestamp.from(ldt).plus(d3)); // 2018-10-20T00:25:04

: 86 .

+4

PS: (P2YT3H10 - 2 , 3 10, ?). , , M () - 10 .


API java.time ISO8601 2 :

  • java.time.Period, (, )
  • java.time.Duration, * (, javadoc: " , , " ).

, ISO8601 .

ThreeTen Extra, API java.time. org.threeten.extra.PeriodDuration , ISO8601:

PeriodDuration pd = PeriodDuration.parse("P2YT3H10M");

Period Duration:

System.out.println(pd.getPeriod()); // P2Y
System.out.println(pd.getDuration()); // PT3H10M

String Period, Duration :

// split in 2 parts
String input = "P2YT3H10M";
String[] v = input.split("T");
Period p;
Duration d;
if (v.length == 1) { // has only date-based fields
    p = Period.parse(input);
    d = Duration.ZERO;
} else {
    if ("P".equals(v[0])) { // has only time-based fields
        p = Period.ZERO;
    } else {
        p = Period.parse(v[0]);
    }
    d = Duration.parse("PT" + v[1]);
}

* A Duration days, 24 ( Period, 24 - ).

+3

All Articles