|
|
c33735 |
From 6af11cb2cfdf83ce013a531005077259984c55e7 Mon Sep 17 00:00:00 2001
|
|
|
c33735 |
From: Stanislav Ochotnicky <sochotnicky@redhat.com>
|
|
|
c33735 |
Date: Wed, 22 Feb 2012 11:21:06 +0100
|
|
|
c33735 |
Subject: [PATCH 2/2] Fix FastDateFormat for Java 7 behaviour
|
|
|
c33735 |
|
|
|
c33735 |
Backported from 1146138
|
|
|
c33735 |
See https://issues.apache.org/jira/browse/LANG-719 for more information
|
|
|
c33735 |
---
|
|
|
c33735 |
.../apache/commons/lang/time/FastDateFormat.java | 14 ++++++++++----
|
|
|
c33735 |
.../commons/lang/time/FastDateFormatTest.java | 5 +++--
|
|
|
c33735 |
2 files changed, 13 insertions(+), 6 deletions(-)
|
|
|
c33735 |
|
|
|
c33735 |
diff --git a/src/main/java/org/apache/commons/lang/time/FastDateFormat.java b/src/main/java/org/apache/commons/lang/time/FastDateFormat.java
|
|
|
c33735 |
index 2ca7e5c..b7e19ec 100644
|
|
|
c33735 |
--- a/src/main/java/org/apache/commons/lang/time/FastDateFormat.java
|
|
|
c33735 |
+++ b/src/main/java/org/apache/commons/lang/time/FastDateFormat.java
|
|
|
c33735 |
@@ -49,7 +49,7 @@ import org.apache.commons.lang.text.StrBuilder;
|
|
|
c33735 |
*
|
|
|
c33735 |
*
|
|
|
c33735 |
* Only formatting is supported, but all patterns are compatible with
|
|
|
c33735 |
- * SimpleDateFormat (except time zones - see below).
|
|
|
c33735 |
+ * SimpleDateFormat (except time zones and some year patterns - see below).
|
|
|
c33735 |
*
|
|
|
c33735 |
* Java 1.4 introduced a new pattern letter, 'Z' , to represent
|
|
|
c33735 |
* time zones in RFC822 format (eg. +0800 or -1100 ).
|
|
|
c33735 |
@@ -60,6 +60,12 @@ import org.apache.commons.lang.text.StrBuilder;
|
|
|
c33735 |
* This introduces a minor incompatibility with Java 1.4, but at a gain of
|
|
|
c33735 |
* useful functionality.
|
|
|
c33735 |
*
|
|
|
c33735 |
+ * Javadoc cites for the year pattern: For formatting, if the number of
|
|
|
c33735 |
+ * pattern letters is 2, the year is truncated to 2 digits; otherwise it is
|
|
|
c33735 |
+ * interpreted as a number. Starting with Java 1.7 a pattern of 'Y' or
|
|
|
c33735 |
+ * 'YYY' will be formatted as '2003', while it was '03' in former Java
|
|
|
c33735 |
+ * versions. FastDateFormat implements the behavior of Java 7.
|
|
|
c33735 |
+ *
|
|
|
c33735 |
* @author Apache Software Foundation
|
|
|
c33735 |
* @author TeaTrove project
|
|
|
c33735 |
* @author Brian S O'Neill
|
|
|
c33735 |
@@ -606,10 +612,10 @@ public class FastDateFormat extends Format {
|
|
|
c33735 |
rule = new TextField(Calendar.ERA, ERAs);
|
|
|
c33735 |
break;
|
|
|
c33735 |
case 'y': // year (number)
|
|
|
c33735 |
- if (tokenLen >= 4) {
|
|
|
c33735 |
- rule = selectNumberRule(Calendar.YEAR, tokenLen);
|
|
|
c33735 |
- } else {
|
|
|
c33735 |
+ if (tokenLen == 2) {
|
|
|
c33735 |
rule = TwoDigitYearField.INSTANCE;
|
|
|
c33735 |
+ } else {
|
|
|
c33735 |
+ rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
|
|
|
c33735 |
}
|
|
|
c33735 |
break;
|
|
|
c33735 |
case 'M': // month in year (text and number)
|
|
|
c33735 |
diff --git a/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java b/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java
|
|
|
c33735 |
index 8232747..bd4d664 100644
|
|
|
c33735 |
--- a/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java
|
|
|
c33735 |
+++ b/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java
|
|
|
c33735 |
@@ -230,8 +230,9 @@ public class FastDateFormatTest extends TestCase {
|
|
|
c33735 |
" dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
|
|
|
c33735 |
fdf = FastDateFormat.getInstance(pattern);
|
|
|
c33735 |
sdf = new SimpleDateFormat(pattern);
|
|
|
c33735 |
- assertEquals(sdf.format(date1), fdf.format(date1));
|
|
|
c33735 |
- assertEquals(sdf.format(date2), fdf.format(date2));
|
|
|
c33735 |
+ // SDF bug fix starting with Java 7
|
|
|
c33735 |
+ assertEquals(sdf.format(date1).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1));
|
|
|
c33735 |
+ assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
|
|
|
c33735 |
|
|
|
c33735 |
} finally {
|
|
|
c33735 |
Locale.setDefault(realDefaultLocale);
|
|
|
c33735 |
--
|
|
|
c33735 |
1.7.7.6
|
|
|
c33735 |
|