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 2018/01/05 14:29:08 UTC
[2/2] groovy git commit: GROOVY-8432: Weird behavior parsing dates
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/f3f98e7b
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f3f98e7b
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f3f98e7b
Branch: refs/heads/GROOVY_2_4_X
Commit: f3f98e7b2b2462d48a6ec2c6f1cce2336e6b6004
Parents: 7fb520f
Author: sunlan <su...@apache.org>
Authored: Fri Jan 5 21:42:55 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Fri Jan 5 22:27:58 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/f3f98e7b/src/main/org/codehaus/groovy/runtime/DateGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DateGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DateGroovyMethods.java
index 1c30802..822bdc5 100644
--- a/src/main/org/codehaus/groovy/runtime/DateGroovyMethods.java
+++ b/src/main/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/f3f98e7b/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()));
+ }
+}