groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [3/3] groovy git commit: GROOVY-8432: Weird behavior parsing dates
Date Fri, 05 Jan 2018 14:20:41 GMT
GROOVY-8432: Weird behavior parsing dates

(cherry picked from commit 033acfb)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/acbadf18
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/acbadf18
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/acbadf18

Branch: refs/heads/GROOVY_2_5_X
Commit: acbadf18ef13fa44220a8db7279a0fc0d5cd0d55
Parents: b95e222
Author: sunlan <sunlan@apache.org>
Authored: Fri Jan 5 21:42:55 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Fri Jan 5 22:20:31 2018 +0800

----------------------------------------------------------------------
 .../groovy/runtime/DateGroovyMethods.java       | 12 ++--
 .../groovy/runtime/DateGroovyMethodsTest.java   | 59 ++++++++++++++++++++
 2 files changed, 65 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/acbadf18/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
index 1c30802..822bdc5 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
@@ -316,7 +316,7 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
      */
     public static Calendar next(Calendar self) {
         Calendar result = (Calendar) self.clone();
-        result.add(Calendar.DAY_OF_YEAR, 1);
+        result.add(Calendar.DATE, 1);
         return result;
     }
 
@@ -329,7 +329,7 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
      */
     public static Calendar previous(Calendar self) {
         Calendar result = (Calendar) self.clone();
-        result.add(Calendar.DAY_OF_YEAR, -1);
+        result.add(Calendar.DATE, -1);
         return result;
     }
 
@@ -375,9 +375,9 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.0
      */
     public static Date plus(Date self, int days) {
-        Calendar calendar = (Calendar) Calendar.getInstance().clone();
+        Calendar calendar = Calendar.getInstance();
         calendar.setTime(self);
-        calendar.add(Calendar.DAY_OF_YEAR, days);
+        calendar.add(Calendar.DATE, days);
         return calendar.getTime();
     }
 
@@ -401,9 +401,9 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
      * @return the new Timestamp
      */
     public static Timestamp plus(Timestamp self, int days) {
-        Calendar calendar = (Calendar) Calendar.getInstance().clone();
+        Calendar calendar = Calendar.getInstance();
         calendar.setTime(self);
-        calendar.add(Calendar.DAY_OF_YEAR, days);
+        calendar.add(Calendar.DATE, days);
         Timestamp ts = new Timestamp(calendar.getTime().getTime());
         ts.setNanos(self.getNanos());
         return ts;

http://git-wip-us.apache.org/repos/asf/groovy/blob/acbadf18/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java b/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java
new file mode 100644
index 0000000..c34ec60
--- /dev/null
+++ b/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.codehaus.groovy.runtime;
+
+import org.junit.Test;
+
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+
+import static org.junit.Assert.assertEquals;
+
+public class DateGroovyMethodsTest {
+    @Test
+    public void minus() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+        assertEquals("20171231", sdf.format(DateGroovyMethods.minus(sdf.parse("20180101"),
1)));
+    }
+
+    @Test
+    public void plus() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+        assertEquals("20180101", sdf.format(DateGroovyMethods.plus(new Timestamp(sdf.parse("20171231").getTime()),
1)));
+    }
+
+    @Test
+    public void next() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(sdf.parse("20171231"));
+        assertEquals("20180101", sdf.format(DateGroovyMethods.next(calendar).getTime()));
+    }
+
+    @Test
+    public void previous() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(sdf.parse("20180101"));
+        assertEquals("20171231", sdf.format(DateGroovyMethods.previous(calendar).getTime()));
+    }
+}


Mime
View raw message