HumanDuration { public static Duration parse(final String duration) { final Matcher matcher = pattern.matcher(duration); final long value; if (!matcher.find()) { throw new DateTimeParseException("Invalid time duration string.", duration, 0); } value = Long.parseLong(matcher.group(1)); if (null == matcher.group(2)) { return Duration.ofMillis(value); } switch (matcher.group(2).toLowerCase()) { case "d": return Duration.ofDays(value); case "h": return Duration.ofHours(value); case "m": return Duration.ofMinutes(value); case "s": return Duration.ofSeconds(value); default: return Duration.ofMillis(value); } }  static Duration parse(final String duration); static String format(final Duration duration);  }
@Test(expected = DateTimeParseException.class) public void testParseThrowsExceptionForInvalidUnit() { parse("500y"); }
@Test public void testParse() { Assert.assertEquals(500, parse("500ms").toMillis()); Assert.assertEquals(60, parse("60s").getSeconds()); Assert.assertEquals(60, parse("60m").toMinutes()); Assert.assertEquals(1, parse("1h").toHours()); Assert.assertEquals(1, parse("1d").toDays()); }
@Test public void testParseDefaultsToMilliseconds() { Assert.assertEquals(500, parse("500").toMillis()); }
@Test(expected = DateTimeParseException.class) public void testParseThrowsExceptionForInvalidValue() { parse("ms"); }