@Test public void testInvalidValidDurations() throws Exception { assertThrows(() -> parseDuration(""), DateTimeParseException.class); assertThrows(() -> parseDuration("17"), DateTimeParseException.class); assertThrows(() -> parseDuration("1 7s"), DateTimeParseException.class); assertThrows(() -> parseDuration("-4s"), DateTimeParseException.class); assertThrows(() -> parseDuration("foobar"), DateTimeParseException.class); }
public static Duration parseDuration(CharSequence text) { Matcher matcher = PATTERN.matcher(text); if (!matcher.matches()) { throw new DateTimeParseException("Invalid duration", text, 0); } String d = matcher.group("days"); String h = matcher.group("hours"); String m = matcher.group("minutes"); String s = matcher.group("seconds"); if (d == null && h == null && m == null && s == null) { throw new DateTimeParseException("Invalid duration", text, 0); } return Duration .ofDays(d == null ? 0 : Long.parseLong(d)) .plusHours(h == null ? 0 : Long.parseLong(h)) .plusMinutes(m == null ? 0 : Long.parseLong(m)) .plusSeconds(s == null ? 0 : Long.parseLong(s)); }
Durations { public static Duration parseDuration(CharSequence text) { Matcher matcher = PATTERN.matcher(text); if (!matcher.matches()) { throw new DateTimeParseException("Invalid duration", text, 0); } String d = matcher.group("days"); String h = matcher.group("hours"); String m = matcher.group("minutes"); String s = matcher.group("seconds"); if (d == null && h == null && m == null && s == null) { throw new DateTimeParseException("Invalid duration", text, 0); } return Duration .ofDays(d == null ? 0 : Long.parseLong(d)) .plusHours(h == null ? 0 : Long.parseLong(h)) .plusMinutes(m == null ? 0 : Long.parseLong(m)) .plusSeconds(s == null ? 0 : Long.parseLong(s)); } }
Durations { public static Duration parseDuration(CharSequence text) { Matcher matcher = PATTERN.matcher(text); if (!matcher.matches()) { throw new DateTimeParseException("Invalid duration", text, 0); } String d = matcher.group("days"); String h = matcher.group("hours"); String m = matcher.group("minutes"); String s = matcher.group("seconds"); if (d == null && h == null && m == null && s == null) { throw new DateTimeParseException("Invalid duration", text, 0); } return Duration .ofDays(d == null ? 0 : Long.parseLong(d)) .plusHours(h == null ? 0 : Long.parseLong(h)) .plusMinutes(m == null ? 0 : Long.parseLong(m)) .plusSeconds(s == null ? 0 : Long.parseLong(s)); }  }
Durations { public static Duration parseDuration(CharSequence text) { Matcher matcher = PATTERN.matcher(text); if (!matcher.matches()) { throw new DateTimeParseException("Invalid duration", text, 0); } String d = matcher.group("days"); String h = matcher.group("hours"); String m = matcher.group("minutes"); String s = matcher.group("seconds"); if (d == null && h == null && m == null && s == null) { throw new DateTimeParseException("Invalid duration", text, 0); } return Duration .ofDays(d == null ? 0 : Long.parseLong(d)) .plusHours(h == null ? 0 : Long.parseLong(h)) .plusMinutes(m == null ? 0 : Long.parseLong(m)) .plusSeconds(s == null ? 0 : Long.parseLong(s)); }  static Duration parseDuration(CharSequence text); static String formatDuration(Duration duration); }
Durations { public static Duration parseDuration(CharSequence text) { Matcher matcher = PATTERN.matcher(text); if (!matcher.matches()) { throw new DateTimeParseException("Invalid duration", text, 0); } String d = matcher.group("days"); String h = matcher.group("hours"); String m = matcher.group("minutes"); String s = matcher.group("seconds"); if (d == null && h == null && m == null && s == null) { throw new DateTimeParseException("Invalid duration", text, 0); } return Duration .ofDays(d == null ? 0 : Long.parseLong(d)) .plusHours(h == null ? 0 : Long.parseLong(h)) .plusMinutes(m == null ? 0 : Long.parseLong(m)) .plusSeconds(s == null ? 0 : Long.parseLong(s)); }  static Duration parseDuration(CharSequence text); static String formatDuration(Duration duration);  }