apache-commons-lang3/0001-Fix-parsing-of-ISO-dates-with-UTC-TZ.patch
2016-02-15 16:15:15 +01:00

63 lines
3.1 KiB
Diff

From 5986eae18fc6006ac4b39c35a7cb7ced8e70c61e Mon Sep 17 00:00:00 2001
From: Michael Simacek <msimacek@redhat.com>
Date: Mon, 15 Feb 2016 15:02:45 +0100
Subject: [PATCH] Fix parsing of ISO dates with UTC TZ
---
.../java/org/apache/commons/lang3/time/DateUtils.java | 3 ++-
.../org/apache/commons/lang3/time/DateUtilsTest.java | 17 ++++++++++++-----
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/time/DateUtils.java b/src/main/java/org/apache/commons/lang3/time/DateUtils.java
index 7535b8b..8312031 100644
--- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java
@@ -390,7 +390,8 @@ public class DateUtils {
String str2 = str;
// LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
if (parsePattern.endsWith("ZZ")) {
- str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
+ str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
+ str2 = str2.replaceAll("Z$", "+0000");
}
final Date date = parser.parse(str2, pos);
diff --git a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java
index 7ef0b8a..a20cf3f 100644
--- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java
@@ -30,6 +30,7 @@ import java.lang.reflect.Modifier;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@@ -1218,11 +1219,17 @@ public class DateUtilsTest {
// http://issues.apache.org/jira/browse/LANG-530
@Test
public void testLang530() throws ParseException {
- final Date d = new Date();
- final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
- final Date d2 = DateUtils.parseDate(isoDateStr, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() });
- // the format loses milliseconds so have to reintroduce them
- assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000);
+ for (long i = 1455545305000L; i <= 1455545306000L; i += 43) {
+ for (String id : ZoneId.getAvailableZoneIds()) {
+ final Date d = new Date(i);
+ FastDateFormat fmt = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ", TimeZone.getTimeZone(id));
+ final String isoDateStr = fmt.format(d);
+ final Date d2 = DateUtils.parseDate(isoDateStr, new String[] { fmt.getPattern() });
+ // the format loses milliseconds so have to reintroduce them
+ assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(),
+ d2.getTime() + d.getTime() % 1000);
+ }
+ }
}
/**
--
2.5.0