Interval implements TemporalAmount { public static Interval parse(final String text) { String sign = ""; int startOffset = 0; if (text.startsWith("-")) { startOffset = 1; sign = "-"; } else if (text.startsWith("+")) { startOffset = 1; } final int durationOffset = text.indexOf('T'); if (durationOffset == -1) { return new Interval(Period.parse(text), Duration.ZERO); } else if (durationOffset == startOffset + 1) { return new Interval(Period.ZERO, Duration.parse(text)); } return new Interval( Period.parse(text.substring(0, durationOffset)), Duration.parse(sign + "P" + text.substring(durationOffset))); } Interval(final Period period, final Duration duration);  Interval(final Duration duration);  Interval(final Period period); Period getPeriod(); Duration getDuration(); long toEpochMilli(final long fromEpochMilli); @Override long get(final TemporalUnit unit); @Override List<TemporalUnit> getUnits(); @Override Temporal addTo(final Temporal temporal); @Override Temporal subtractFrom(final Temporal temporal); @Override int hashCode(); @Override boolean equals(final Object o); @Override String toString(); static Interval parse(final String text);  }
@Test public void shouldParseDurationWithNoPeriod() { final String text = "PT05S"; final Interval expected = new Interval(Period.ZERO, Duration.ofSeconds(5)); final Interval interval = Interval.parse(text); assertThat(interval).isEqualTo(expected); }
@Test public void shouldParsePeriodWithNoDuration() { final String text = "P1Y2M4D"; final Interval expected = new Interval(Period.of(1, 2, 4), Duration.ZERO); final Interval interval = Interval.parse(text); assertThat(interval).isEqualTo(expected); }
@Test public void shouldParseWithPeriodAndDuration() { final String text = "P1Y2M4DT1H2M3S"; final Interval expected = new Interval( Period.of(1, 2, 4), Duration.ofHours(1).plus(Duration.ofMinutes(2)).plus(Duration.ofSeconds(3))); final Interval interval = Interval.parse(text); assertThat(interval).isEqualTo(expected); }
@Test public void shouldParseNegativeInterval() { final String text = "-P1Y2M4DT1H2M3S"; final Interval expected = new Interval( Period.of(-1, -2, -4), Duration.ofHours(-1).plus(Duration.ofMinutes(-2)).plus(Duration.ofSeconds(-3))); final Interval interval = Interval.parse(text); assertThat(interval).isEqualTo(expected); }
@Test public void shouldParsePositiveInterval() { final String text = "+P1Y2M4DT1H2M3S"; final Interval expected = new Interval( Period.of(1, 2, 4), Duration.ofHours(1).plus(Duration.ofMinutes(2)).plus(Duration.ofSeconds(3))); final Interval interval = Interval.parse(text); assertThat(interval).isEqualTo(expected); }
@Test public void shouldFailToParseWrongPeriod() { final String text = "P,DT1H2M3S"; assertThatThrownBy(() -> Interval.parse(text)).isInstanceOf(DateTimeParseException.class); }
@Test public void shouldFailToParseWrongDuration() { final String text = "P1Y2D3MDT!"; assertThatThrownBy(() -> Interval.parse(text)).isInstanceOf(DateTimeParseException.class); }
@Test public void shouldFailToParseWrongPeriodAndDuration() { final String text = "PGKLDT4.?"; assertThatThrownBy(() -> Interval.parse(text)).isInstanceOf(DateTimeParseException.class); }
@Test public void shouldFailToParseIfNotStartingWithP() { final String text = "T01S"; assertThatThrownBy(() -> Interval.parse(text)).isInstanceOf(DateTimeParseException.class); }
@Test public void shouldFailToParseEmptyString() { assertThatThrownBy(() -> Interval.parse("")).isInstanceOf(DateTimeParseException.class); }