Duration implements Comparable<Duration> { static java.time.Duration parse(String value) { Objects.requireNonNull(value, "Null 'value' argument"); if (value.length() == 0) { throw new IllegalArgumentException("Empty 'value' argument"); } Matcher matcher = TOKENIZER.matcher(value); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid Duration format: " + value); } TemporalUnit unit = parseUnit(matcher.group(2)); double amount = parseAmount(matcher.group(1)); return createDuration(amount, unit); } Duration(String value);  Duration(long value); @Override boolean equals(Object obj); @Override int hashCode(); java.time.Duration getDuration(); @Override int compareTo(Duration o); @Override String toString(); static final Duration ZERO; }
@Test public void testParse_Millis() { assertEquals(java.time.Duration.ofMillis(4), Duration.parse("4ms")); assertEquals(java.time.Duration.ofSeconds(4), Duration.parse("4000 ms")); }
@Test public void testParse_Seconds() { assertEquals(java.time.Duration.ofSeconds(4), Duration.parse("4sec")); assertEquals(java.time.Duration.ofSeconds(4), Duration.parse("4 sec")); assertEquals(java.time.Duration.ofSeconds(4), Duration.parse("4s")); assertEquals(java.time.Duration.ofSeconds(40), Duration.parse("40 seconds")); }
@Test public void testParse_Minutes() { assertEquals(java.time.Duration.ofMinutes(4), Duration.parse("4min")); assertEquals(java.time.Duration.ofMinutes(1), Duration.parse("1 minute")); assertEquals(java.time.Duration.ofHours(1), Duration.parse("60 minutes")); }
@Test public void testParse_Hours() { assertEquals(java.time.Duration.ofHours(4), Duration.parse("4hours")); assertEquals(java.time.Duration.ofMinutes(60), Duration.parse("1 hr")); assertEquals(java.time.Duration.ofHours(1), Duration.parse("1 hour")); assertEquals(java.time.Duration.ofHours(5), Duration.parse("5 hrs")); assertEquals(java.time.Duration.ofHours(5), Duration.parse("5 h")); }
@Test public void testParse_Days() { assertEquals(java.time.Duration.ofDays(4), Duration.parse("4d")); assertEquals(java.time.Duration.ofDays(60), Duration.parse("60 days")); assertEquals(java.time.Duration.ofHours(24), Duration.parse("1 day")); }
@Test public void testParse_Fract() { assertEquals(java.time.Duration.ofMillis(1100), Duration.parse("1.1s")); assertEquals(java.time.Duration.ofMillis(1235), Duration.parse("1.23456 sec")); assertEquals(java.time.Duration.ofMillis(1600), Duration.parse("1.6 sec")); assertEquals(java.time.Duration.ofMillis(123), Duration.parse(".123 s")); assertEquals(java.time.Duration.ofMillis(543), Duration.parse("0.5433 s")); assertEquals(java.time.Duration.ofMillis(126000), Duration.parse("2.1 min")); assertEquals(java.time.Duration.ofMillis(12816000), Duration.parse("3.56 h")); assertEquals(java.time.Duration.ofMillis(95040000), Duration.parse("1.1days")); }
@Test public void testParse_Null() { assertThrows(NullPointerException.class, () -> Duration.parse(null)); }
@Test public void testParse_FractMs() { assertThrows(IllegalArgumentException.class, () -> Duration.parse("1.1 ms")); }
@Test public void testParse_Empty() { assertThrows(IllegalArgumentException.class, () -> Duration.parse("")); }
@Test public void testParse_Invalid1() { assertThrows(IllegalArgumentException.class, () -> Duration.parse("4 nosuchthing")); }
@Test public void testParse_Invalid2() { assertThrows(IllegalArgumentException.class, () -> Duration.parse("not_a_number sec")); }