diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bb5f6451..f57c5b6d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -12,6 +12,10 @@ Add PeriodDuration, combining Period and Duration. See #74. + + Extend formats Parsed by Interval, allowing years, months, weeks and days. + See #70. + Add Seconds temporal amount class. See #73. diff --git a/src/main/java/org/threeten/extra/Interval.java b/src/main/java/org/threeten/extra/Interval.java index f4b900fb..d4849c7e 100644 --- a/src/main/java/org/threeten/extra/Interval.java +++ b/src/main/java/org/threeten/extra/Interval.java @@ -132,12 +132,11 @@ public static Interval of(Instant startInclusive, Duration duration) { *
  • a representations of an {@link OffsetDateTime}, followed by a forward slash, * followed by a representation of a {@link OffsetDateTime} *
  • a representation of an {@link OffsetDateTime}, followed by a forward slash, - * followed by a representation of a {@link Duration} - *
  • a representation of a {@link Duration}, followed by a forward slash, + * followed by a representation of a {@link PeriodDuration} + *
  • a representation of a {@link PeriodDuration}, followed by a forward slash, * followed by a representation of an {@link OffsetDateTime} * * - * * @param text the text to parse, not null * @return the parsed interval, not null * @throws DateTimeParseException if the text cannot be parsed @@ -149,21 +148,21 @@ public static Interval parse(CharSequence text) { char firstChar = text.charAt(0); if (firstChar == 'P' || firstChar == 'p') { // duration followed by instant - Duration duration = Duration.parse(text.subSequence(0, i)); - Instant end = OffsetDateTime.parse(text.subSequence(i + 1, text.length())).toInstant(); - return Interval.of(end.minus(duration), end); + PeriodDuration amount = PeriodDuration.parse(text.subSequence(0, i)); + OffsetDateTime end = OffsetDateTime.parse(text.subSequence(i + 1, text.length())); + return Interval.of(end.minus(amount).toInstant(), end.toInstant()); } else { // instant followed by instant or duration - Instant start = OffsetDateTime.parse(text.subSequence(0, i)).toInstant(); + OffsetDateTime start = OffsetDateTime.parse(text.subSequence(0, i)); if (i + 1 < text.length()) { char c = text.charAt(i + 1); if (c == 'P' || c == 'p') { - Duration duration = Duration.parse(text.subSequence(i + 1, text.length())); - return Interval.of(start, start.plus(duration)); + PeriodDuration amount = PeriodDuration.parse(text.subSequence(i + 1, text.length())); + return Interval.of(start.toInstant(), start.plus(amount).toInstant()); } } - Instant end = OffsetDateTime.parse(text.subSequence(i + 1, text.length())).toInstant(); - return Interval.of(start, end); + OffsetDateTime end = OffsetDateTime.parse(text.subSequence(i + 1, text.length())); + return Interval.of(start.toInstant(), end.toInstant()); } } } diff --git a/src/test/java/org/threeten/extra/TestInterval.java b/src/test/java/org/threeten/extra/TestInterval.java index 7a735b70..12415371 100644 --- a/src/test/java/org/threeten/extra/TestInterval.java +++ b/src/test/java/org/threeten/extra/TestInterval.java @@ -32,6 +32,7 @@ package org.threeten.extra; import static java.time.temporal.ChronoUnit.HOURS; +import static java.time.temporal.ChronoUnit.MONTHS; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -165,6 +166,12 @@ public void test_parse_CharSequence_DurationInstant() { assertEquals(test.getEnd(), NOW2); } + public void test_parse_CharSequence_PeriodDurationInstant() { + Interval test = Interval.parse("P6MT5H/" + NOW2); + assertEquals(test.getStart(), NOW2.atZone(ZoneOffset.UTC).minus(6, MONTHS).minus(5, HOURS).toInstant()); + assertEquals(test.getEnd(), NOW2); + } + public void test_parse_CharSequence_DurationInstant_caseInsensitive() { Interval test = Interval.parse("pt6h/" + NOW2); assertEquals(test.getStart(), NOW2.minus(6, HOURS));