@Test public void testParse() { assertEquals(Duration.ofSeconds(20).plusMillis(345), parse("PT20.345S")); assertEquals(Duration.ofMinutes(15), parse("PT15M")); assertEquals(Duration.ofHours(10), parse("PT10H")); assertEquals(Duration.ofDays(2), parse("P2D")); assertEquals(Duration.ofDays(2).plusHours(3).plusMinutes(4), parse("P2DT3H4M")); assertEquals(Duration.ofHours(-6).plusMinutes(3), parse("PT-6H3M")); assertEquals(Duration.ofHours(-6).plusMinutes(-3), parse("-PT6H3M")); assertEquals(Duration.ofHours(6).plusMinutes(-3), parse("-PT-6H+3M")); assertEquals(Duration.ofMillis(123), parse("123ms")); assertEquals(Duration.ofSeconds(88), parse("88s")); assertEquals(Duration.ofMinutes(15), parse("15m")); assertEquals(Duration.ofHours(10), parse("10h")); assertEquals(Duration.ofDays(2), parse("2d")); assertEquals(Duration.ofDays(2).plusHours(3).plusMinutes(4).plusSeconds(5).plusMillis(6), parse("2d3h4m5s6ms")); try { parse("1 day"); fail(); } catch (DateTimeParseException e) { assertEquals("Text cannot be parsed to a Duration", e.getMessage()); assertEquals("1 day", e.getParsedString()); } }
public static Duration parse(String value) { if (value.startsWith("P") || value.startsWith("-P")) { return Duration.parse(value); } Matcher matcher = DURATION_SIMPLE_FORMAT.matcher(value); if (matcher.matches()) { long days = 0; long hours = 0; long minutes = 0; long seconds = 0; long millis = 0; if (matcher.group(1) != null) { days = Long.parseLong(matcher.group(1)); } if (matcher.group(2) != null) { hours = Long.parseLong(matcher.group(2)); } if (matcher.group(3) != null) { minutes = Long.parseLong(matcher.group(3)); } if (matcher.group(4) != null) { seconds = Long.parseLong(matcher.group(4)); } if (matcher.group(5) != null) { millis = Long.parseLong(matcher.group(5)); } return Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds).plusMillis(millis); } throw new DateTimeParseException("Text cannot be parsed to a Duration", value, 0); }
DurationUtils { public static Duration parse(String value) { if (value.startsWith("P") || value.startsWith("-P")) { return Duration.parse(value); } Matcher matcher = DURATION_SIMPLE_FORMAT.matcher(value); if (matcher.matches()) { long days = 0; long hours = 0; long minutes = 0; long seconds = 0; long millis = 0; if (matcher.group(1) != null) { days = Long.parseLong(matcher.group(1)); } if (matcher.group(2) != null) { hours = Long.parseLong(matcher.group(2)); } if (matcher.group(3) != null) { minutes = Long.parseLong(matcher.group(3)); } if (matcher.group(4) != null) { seconds = Long.parseLong(matcher.group(4)); } if (matcher.group(5) != null) { millis = Long.parseLong(matcher.group(5)); } return Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds).plusMillis(millis); } throw new DateTimeParseException("Text cannot be parsed to a Duration", value, 0); } }
DurationUtils { public static Duration parse(String value) { if (value.startsWith("P") || value.startsWith("-P")) { return Duration.parse(value); } Matcher matcher = DURATION_SIMPLE_FORMAT.matcher(value); if (matcher.matches()) { long days = 0; long hours = 0; long minutes = 0; long seconds = 0; long millis = 0; if (matcher.group(1) != null) { days = Long.parseLong(matcher.group(1)); } if (matcher.group(2) != null) { hours = Long.parseLong(matcher.group(2)); } if (matcher.group(3) != null) { minutes = Long.parseLong(matcher.group(3)); } if (matcher.group(4) != null) { seconds = Long.parseLong(matcher.group(4)); } if (matcher.group(5) != null) { millis = Long.parseLong(matcher.group(5)); } return Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds).plusMillis(millis); } throw new DateTimeParseException("Text cannot be parsed to a Duration", value, 0); } private  DurationUtils(); }
DurationUtils { public static Duration parse(String value) { if (value.startsWith("P") || value.startsWith("-P")) { return Duration.parse(value); } Matcher matcher = DURATION_SIMPLE_FORMAT.matcher(value); if (matcher.matches()) { long days = 0; long hours = 0; long minutes = 0; long seconds = 0; long millis = 0; if (matcher.group(1) != null) { days = Long.parseLong(matcher.group(1)); } if (matcher.group(2) != null) { hours = Long.parseLong(matcher.group(2)); } if (matcher.group(3) != null) { minutes = Long.parseLong(matcher.group(3)); } if (matcher.group(4) != null) { seconds = Long.parseLong(matcher.group(4)); } if (matcher.group(5) != null) { millis = Long.parseLong(matcher.group(5)); } return Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds).plusMillis(millis); } throw new DateTimeParseException("Text cannot be parsed to a Duration", value, 0); } private  DurationUtils(); static Duration parse(String value); static Duration parsePositive(String value, Duration defaultDuration); }
DurationUtils { public static Duration parse(String value) { if (value.startsWith("P") || value.startsWith("-P")) { return Duration.parse(value); } Matcher matcher = DURATION_SIMPLE_FORMAT.matcher(value); if (matcher.matches()) { long days = 0; long hours = 0; long minutes = 0; long seconds = 0; long millis = 0; if (matcher.group(1) != null) { days = Long.parseLong(matcher.group(1)); } if (matcher.group(2) != null) { hours = Long.parseLong(matcher.group(2)); } if (matcher.group(3) != null) { minutes = Long.parseLong(matcher.group(3)); } if (matcher.group(4) != null) { seconds = Long.parseLong(matcher.group(4)); } if (matcher.group(5) != null) { millis = Long.parseLong(matcher.group(5)); } return Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds).plusMillis(millis); } throw new DateTimeParseException("Text cannot be parsed to a Duration", value, 0); } private  DurationUtils(); static Duration parse(String value); static Duration parsePositive(String value, Duration defaultDuration); static final Pattern DURATION_SIMPLE_FORMAT; }