e12d0f
From 6af11cb2cfdf83ce013a531005077259984c55e7 Mon Sep 17 00:00:00 2001
e12d0f
From: Stanislav Ochotnicky <sochotnicky@redhat.com>
e12d0f
Date: Wed, 22 Feb 2012 11:21:06 +0100
e12d0f
Subject: [PATCH 2/2] Fix FastDateFormat for Java 7 behaviour
e12d0f
e12d0f
Backported from 1146138
e12d0f
See https://issues.apache.org/jira/browse/LANG-719 for more information
e12d0f
---
e12d0f
 .../apache/commons/lang/time/FastDateFormat.java   |   14 ++++++++++----
e12d0f
 .../commons/lang/time/FastDateFormatTest.java      |    5 +++--
e12d0f
 2 files changed, 13 insertions(+), 6 deletions(-)
e12d0f
e12d0f
diff --git a/src/main/java/org/apache/commons/lang/time/FastDateFormat.java b/src/main/java/org/apache/commons/lang/time/FastDateFormat.java
e12d0f
index 2ca7e5c..b7e19ec 100644
e12d0f
--- a/src/main/java/org/apache/commons/lang/time/FastDateFormat.java
e12d0f
+++ b/src/main/java/org/apache/commons/lang/time/FastDateFormat.java
e12d0f
@@ -49,7 +49,7 @@ import org.apache.commons.lang.text.StrBuilder;
e12d0f
  * 

e12d0f
  *
e12d0f
  * 

Only formatting is supported, but all patterns are compatible with

e12d0f
- * SimpleDateFormat (except time zones - see below).

e12d0f
+ * SimpleDateFormat (except time zones and some year patterns - see below).

e12d0f
  *
e12d0f
  * 

Java 1.4 introduced a new pattern letter, 'Z', to represent

e12d0f
  * time zones in RFC822 format (eg. +0800 or -1100).
e12d0f
@@ -60,6 +60,12 @@ import org.apache.commons.lang.text.StrBuilder;
e12d0f
  * This introduces a minor incompatibility with Java 1.4, but at a gain of
e12d0f
  * useful functionality.

e12d0f
  *
e12d0f
+ * 

Javadoc cites for the year pattern: For formatting, if the number of

e12d0f
+ * pattern letters is 2, the year is truncated to 2 digits; otherwise it is
e12d0f
+ * interpreted as a number. Starting with Java 1.7 a pattern of 'Y' or
e12d0f
+ * 'YYY' will be formatted as '2003', while it was '03' in former Java
e12d0f
+ * versions. FastDateFormat implements the behavior of Java 7.

e12d0f
+ *
e12d0f
  * @author Apache Software Foundation
e12d0f
  * @author TeaTrove project
e12d0f
  * @author Brian S O'Neill
e12d0f
@@ -606,10 +612,10 @@ public class FastDateFormat extends Format {
e12d0f
                 rule = new TextField(Calendar.ERA, ERAs);
e12d0f
                 break;
e12d0f
             case 'y': // year (number)
e12d0f
-                if (tokenLen >= 4) {
e12d0f
-                    rule = selectNumberRule(Calendar.YEAR, tokenLen);
e12d0f
-                } else {
e12d0f
+                if (tokenLen == 2) {
e12d0f
                     rule = TwoDigitYearField.INSTANCE;
e12d0f
+                } else {
e12d0f
+                    rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
e12d0f
                 }
e12d0f
                 break;
e12d0f
             case 'M': // month in year (text and number)
e12d0f
diff --git a/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java b/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java
e12d0f
index 8232747..bd4d664 100644
e12d0f
--- a/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java
e12d0f
+++ b/src/test/java/org/apache/commons/lang/time/FastDateFormatTest.java
e12d0f
@@ -230,8 +230,9 @@ public class FastDateFormatTest extends TestCase {
e12d0f
                 " dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
e12d0f
             fdf = FastDateFormat.getInstance(pattern);
e12d0f
             sdf = new SimpleDateFormat(pattern);
e12d0f
-            assertEquals(sdf.format(date1), fdf.format(date1));
e12d0f
-            assertEquals(sdf.format(date2), fdf.format(date2));
e12d0f
+            // SDF bug fix starting with Java 7
e12d0f
+            assertEquals(sdf.format(date1).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1));
e12d0f
+            assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
e12d0f
 
e12d0f
         } finally {
e12d0f
             Locale.setDefault(realDefaultLocale);
e12d0f
-- 
e12d0f
1.7.7.6
e12d0f