diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java b/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java index 8be2e6133..51da48e13 100644 --- a/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java +++ b/src/main/java/org/apache/commons/lang3/time/FastDateFormat.java @@ -47,7 +47,7 @@ *

* *

Only formatting is supported, but all patterns are compatible with - * SimpleDateFormat (except time zones - see below).

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

* *

Java 1.4 introduced a new pattern letter, {@code 'Z'}, to represent * time zones in RFC822 format (eg. {@code +0800} or {@code -1100}). @@ -58,6 +58,12 @@ * This introduces a minor incompatibility with Java 1.4, but at a gain of * useful functionality.

* + *

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

+ * * @since 2.0 * @version $Id$ */ @@ -486,10 +492,10 @@ protected List parsePattern() { rule = new TextField(Calendar.ERA, ERAs); break; case 'y': // year (number) - if (tokenLen >= 4) { - rule = selectNumberRule(Calendar.YEAR, tokenLen); - } else { + if (tokenLen == 2) { rule = TwoDigitYearField.INSTANCE; + } else { + rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen); } break; case 'M': // month in year (text and number) diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java index e4cde1a46..6feb57d1b 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java @@ -216,8 +216,9 @@ public void testFormat() { " dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z"; fdf = FastDateFormat.getInstance(pattern); sdf = new SimpleDateFormat(pattern); - assertEquals(sdf.format(date1), fdf.format(date1)); - assertEquals(sdf.format(date2), fdf.format(date2)); + // SDF bug fix starting with Java 7 + assertEquals(sdf.format(date1).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1)); + assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2)); } finally { Locale.setDefault(realDefaultLocale); TimeZone.setDefault(realDefaultZone);