Adjust FastDateFormat for Java 7 behavior regarding format of the year pattern (LANG-719).

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1146138 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joerg Schaible 2011-07-13 17:01:37 +00:00
parent c023bfb593
commit 34a6449c90
2 changed files with 13 additions and 6 deletions

View File

@ -47,7 +47,7 @@
* </p>
*
* <p>Only formatting is supported, but all patterns are compatible with
* SimpleDateFormat (except time zones - see below).</p>
* SimpleDateFormat (except time zones and some year patterns - see below).</p>
*
* <p>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.</p>
*
* <p>Javadoc cites for the year pattern: <i>For formatting, if the number of
* pattern letters is 2, the year is truncated to 2 digits; otherwise it is
* interpreted as a number.</i> 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.</p>
*
* @since 2.0
* @version $Id$
*/
@ -486,10 +492,10 @@ protected List<Rule> 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)

View File

@ -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);