Duration implements Comparable<Duration> { public double getValue() { return value; } Duration(double value, TimeUnit unit); static Duration nanosSince(long start); static Duration succinctNanos(long nanos); static Duration succinctDuration(double value, TimeUnit unit); long toMillis(); double getValue(); TimeUnit getUnit(); double getValue(TimeUnit timeUnit); long roundTo(TimeUnit timeUnit); Duration convertToMostSuccinctTimeUnit(); @JsonValue @Override String toString(); String toString(TimeUnit timeUnit); @JsonCreator static Duration valueOf(String duration); @Override int compareTo(Duration o); @Override boolean equals(Object o); @Override int hashCode(); static TimeUnit valueOfTimeUnit(String timeUnitString); static String timeUnitToString(TimeUnit timeUnit);  }
@Test public void testGetValue() { double millis = 12346789.0d; Duration duration = new Duration(millis, MILLISECONDS); assertThat(duration.getValue(MILLISECONDS)).isEqualTo(millis); assertThat(duration.getValue(SECONDS)).isCloseTo(millis / 1000, offset(0.001)); assertThat(duration.getValue(MINUTES)).isCloseTo(millis / 1000 / 60, offset(0.001)); assertThat(duration.getValue(HOURS)).isCloseTo(millis / 1000 / 60 / 60, offset(0.001)); assertThat(duration.getValue(DAYS)).isCloseTo(millis / 1000 / 60 / 60 / 24, offset(0.001)); double days = 3.0; duration = new Duration(days, DAYS); assertThat(duration.getValue(DAYS)).isEqualTo(days); assertThat(duration.getValue(HOURS)).isCloseTo(days * 24, offset(0.001)); assertThat(duration.getValue(MINUTES)).isCloseTo(days * 24 * 60, offset(0.001)); assertThat(duration.getValue(SECONDS)).isCloseTo(days * 24 * 60 * 60, offset(0.001)); assertThat(duration.getValue(MILLISECONDS)).isCloseTo(days * 24 * 60 * 60 * 1000, offset(0.001)); }
@Test public void testNanoConversions() { double nanos = 1.0d; Duration duration = new Duration(nanos, NANOSECONDS); assertThat(duration.getValue()).isEqualTo(nanos); assertThat(duration.getValue(NANOSECONDS)).isEqualTo(nanos); assertThat(duration.getValue(MILLISECONDS)).isEqualTo(nanos / 1000000); assertThat(duration.getValue(SECONDS)).isEqualTo(nanos / 1000000 / 1000); assertThat(duration.getValue(MINUTES)).isCloseTo(nanos / 1000000 / 1000 / 60, offset(1.0E10)); assertThat(duration.getValue(HOURS)).isCloseTo(nanos / 1000000 / 1000 / 60 / 60, offset(1.0E10)); assertThat(duration.getValue(DAYS)).isCloseTo(nanos / 1000000 / 1000 / 60 / 60 / 24, offset(1.0E10)); }