You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/10/12 13:03:09 UTC
[groovy] 04/04: GROOVY-9274: Add DGM `minus` to `LocalDate`
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 9541f467e9a718c79200cfa72a2720e30c5897fa
Author: Daniel.Sun <re...@hotmail.com>
AuthorDate: Wed Oct 9 18:05:19 2019 +0800
GROOVY-9274: Add DGM `minus` to `LocalDate`
(cherry picked from commit 37ec9a72b676a550582baa03e70126e2603c9fa0)
---
.../groovy/datetime/extensions/DateTimeExtensions.java | 12 ++++++++++++
.../src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy | 14 ++++++++++++++
2 files changed, 26 insertions(+)
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 @@ public class DateTimeExtensions {
}
/**
+ * 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 @@ class WorkingWithDateTimeTypesTest extends GroovyTestCase {
// 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
+ }
}