You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/07/11 06:36:07 UTC
[groovy] branch GROOVY_2_5_X updated: GROOVY-9274: Add DGM `minus`
to `LocalDate`
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new 7e691fb GROOVY-9274: Add DGM `minus` to `LocalDate`
7e691fb is described below
commit 7e691fb02f1d935a12d072097aea834d0c78ba84
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 0ec66f3..6891090 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 4429351..e19d66a 100644
--- a/subprojects/groovy-datetime/src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy
+++ b/subprojects/groovy-datetime/src/spec/test/gdk/WorkingWithDateTimeTypesTest.groovy
@@ -250,4 +250,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
+ }
}