GROOVY-9274: Add DGM `minus` to `LocalDate`
diff --git a/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java b/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java
index c46789a..85bf09f 100644
--- a/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java
+++ b/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java
@@ -579,6 +579,18 @@
}
/**
+ * Calculates the number of days between two dates
+ *
+ * @param self a LocalDate
+ * @param other the other LocalDate
+ * @return the number of days
+ * @since 3.0.0
+ */
+ public static long minus(final LocalDate self, LocalDate other) {
+ return ChronoUnit.DAYS.between(other, self);
+ }
+
+ /**
* Returns a {@link java.time.LocalDate} one day after this date.
*
* @param self a LocalDate
diff --git a/subprojects/groovy-datetime/src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy b/subprojects/groovy-datetime/src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy
index 14c0191..005c8df 100644
--- a/subprojects/groovy-datetime/src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy
+++ b/subprojects/groovy-datetime/src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy
@@ -252,4 +252,18 @@
// end::to_jsr310_types[]
}
+ void testLocalDateMinusOtherLocalDate() {
+ def date1 = LocalDate.of(2019, Month.OCTOBER, 10)
+ def date2 = LocalDate.of(2019, Month.OCTOBER, 1)
+ assert 9 == date1 - date2
+ assert -9 == date2 - date1
+
+ def date3 = LocalDate.of(2019, Month.OCTOBER, 1)
+ def date4 = LocalDate.of(2019, Month.OCTOBER, 1)
+ assert 0 == date3 - date4
+
+ def date5 = LocalDate.of(2019, Month.OCTOBER, 1)
+ def date6 = LocalDate.of(2019, Month.SEPTEMBER, 30)
+ assert 1 == date5 - date6
+ }
}