From fcdb86f818dd162fa3d43d200c9abb55ead2f614 Mon Sep 17 00:00:00 2001 From: Scott Russell Date: Wed, 30 Oct 2013 21:18:19 +0000 Subject: [PATCH] ICU-10239 make eee[ee] & EEEEE processing in J behave the same as C X-SVN-Rev: 34627 --- .../com/ibm/icu/text/SimpleDateFormat.java | 32 +++++++--- .../test/format/DateFormatRegressionTest.java | 59 +++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java index 283d33ed497..be8470858e2 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/SimpleDateFormat.java @@ -2732,6 +2732,7 @@ public class SimpleDateFormat extends DateFormat { patternCharIndex == 15 /*'h' HOUR1_FIELD*/ || (patternCharIndex == 2 /*'M' MONTH_FIELD*/ && count <= 2) || (patternCharIndex == 26 /*'L' STAND_ALONE_MONTH*/ && count <= 2) || + (patternCharIndex == 19 /*'e' DOW_LOCAL*/ && count <= 2) || patternCharIndex == 1 /*'y' YEAR */ || patternCharIndex == 18 /*'Y' YEAR_WOY */ || patternCharIndex == 30 /*'U' YEAR_NAME_FIELD, falls back to numeric */ || (patternCharIndex == 0 /*'G' ERA */ && isChineseCalendar) || @@ -2916,15 +2917,31 @@ public class SimpleDateFormat extends DateFormat { } cal.set(Calendar.MILLISECOND, value); return pos.getIndex(); + case 19: // 'e' - DOW_LOCAL + if(count <= 2) { // i.e. e/ee + cal.set(field, value); + return pos.getIndex(); + } + // else for eee-eeeeee, fall through to EEE-EEEEEE handling + //$FALL-THROUGH$ case 9: { // 'E' - DAY_OF_WEEK - // Want to be able to parse at least wide, abbrev, short forms. - int newStart = matchString(text, start, Calendar.DAY_OF_WEEK, formatData.weekdays, null, cal); // try EEEE wide - if (newStart > 0) { + // Want to be able to parse at least wide, abbrev, short, and narrow forms. + int newStart = 0; + if ((newStart = matchString(text, start, Calendar.DAY_OF_WEEK, formatData.weekdays, null, cal)) > 0) { // try EEEE wide return newStart; - } else if ((newStart = matchString(text, start, Calendar.DAY_OF_WEEK, formatData.shortWeekdays, null, cal)) > 0) { // try EEE abbrev + } + if ((newStart = matchString(text, start, Calendar.DAY_OF_WEEK, formatData.shortWeekdays, null, cal)) > 0) { // try EEE abbrev return newStart; - } else if (formatData.shorterWeekdays != null) { - return matchString(text, start, Calendar.DAY_OF_WEEK, formatData.shorterWeekdays, null, cal); // try EEEEEE short + } + if (formatData.shorterWeekdays != null) { + if((newStart = matchString(text, start, Calendar.DAY_OF_WEEK, formatData.shorterWeekdays, null, cal)) > 0) { // try EEEEEE short + return newStart; + } + } + if (formatData.narrowWeekdays != null) { + if((newStart = matchString(text, start, Calendar.DAY_OF_WEEK, formatData.narrowWeekdays, null, cal)) > 0) { // try EEEEE narrow + return newStart; + } } return newStart; } @@ -3133,8 +3150,7 @@ public class SimpleDateFormat extends DateFormat { // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH // case 12: // 'w' - WEEK_OF_YEAR // case 13: // 'W' - WEEK_OF_MONTH - // case 16: // 'K' - HOUR (0..11) - // case 19: // 'e' - DOW_LOCAL + // case 16: // 'K' - HOUR (0..11) // case 20: // 'u' - EXTENDED_YEAR // case 21: // 'g' - JULIAN_DAY // case 22: // 'A' - MILLISECONDS_IN_DAY diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java index 3e9f02cd06b..68f6bbcb76a 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateFormatRegressionTest.java @@ -1216,5 +1216,64 @@ public class DateFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk { } errln("No exception thrown at all for bad pattern!"); } + + public void TestT10239() { + + class TestDateFormatItem { + public String parseString; + public String pattern; + public String expectedResult; // null indicates expected error + // Simple constructor + public TestDateFormatItem(String parString, String patt, String expResult) { + pattern = patt; + parseString = parString; + expectedResult = expResult; + } + }; + + final TestDateFormatItem[] items = { + // parse String pattern expected result + new TestDateFormatItem("1 Oct 13 2013", "e MMM dd yyyy", "1 Oct 13 2013"), + new TestDateFormatItem("02 Oct 14 2013", "ee MMM dd yyyy", "02 Oct 14 2013"), + new TestDateFormatItem("Tue Oct 15 2013", "eee MMM dd yyyy", "Tue Oct 15 2013"), + new TestDateFormatItem("Wednesday Oct 16 2013", "eeee MMM dd yyyy", "Wednesday Oct 16 2013"), + new TestDateFormatItem("Th Oct 17 2013", "eeeeee MMM dd yyyy", "Th Oct 17 2013"), + new TestDateFormatItem("Fr Oct 18 2013", "EEEEEE MMM dd yyyy", "Fr Oct 18 2013"), + new TestDateFormatItem("S Oct 19 2013", "eeeee MMM dd yyyy", "S Oct 19 2013"), + new TestDateFormatItem("S Oct 20 2013", "EEEEE MMM dd yyyy", "S Oct 20 2013"), + }; + + StringBuffer result = new StringBuffer(); + Date d = new Date(); + Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US); + SimpleDateFormat sdfmt = new SimpleDateFormat(); + ParsePosition p = new ParsePosition(0); + for (TestDateFormatItem item: items) { + cal.clear(); + sdfmt.setCalendar(cal); + sdfmt.applyPattern(item.pattern); + result.setLength(0); + p.setIndex(0); + p.setErrorIndex(-1); + d = sdfmt.parse(item.parseString, p); + if(item.expectedResult == null) { + if(p.getErrorIndex() != -1) + continue; + else + errln("error: unexpected parse success..."+item.parseString + " should have failed"); + } + if(p.getErrorIndex() != -1) { + errln("error: parse error for string " +item.parseString + " against pattern " + item.pattern + " -- idx["+p.getIndex()+"] errIdx["+p.getErrorIndex()+"]"); + continue; + } + cal.setTime(d); + result = sdfmt.format(cal, result, new FieldPosition(0)); + if(!result.toString().equalsIgnoreCase(item.expectedResult)) { + errln("error: unexpected format result. expected - " + item.expectedResult + " but result was - " + result); + } else { + logln("formatted results match! - " + result.toString()); + } + } + } } -- 2.40.0