LANG-828 FastDateParser does not handle non-Gregorian calendars properly

Fix bug in Java 7 (Locale.toString() format has changed)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1390189 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-25 22:12:14 +00:00
parent 73ea9c43c6
commit e582456625
2 changed files with 9 additions and 5 deletions

View File

@ -76,6 +76,8 @@ public class FastDateParser implements DateParser, Serializable {
private static final ConcurrentMap<Locale,TimeZoneStrategy> tzsCache= private static final ConcurrentMap<Locale,TimeZoneStrategy> tzsCache=
new ConcurrentHashMap<Locale,TimeZoneStrategy>(3); new ConcurrentHashMap<Locale,TimeZoneStrategy>(3);
static final Locale JAPANESE_IMPERIAL = new Locale("ja","JP","JP");
// defining fields // defining fields
private final String pattern; private final String pattern;
private final TimeZone timeZone; private final TimeZone timeZone;
@ -123,10 +125,12 @@ public class FastDateParser implements DateParser, Serializable {
throw new IllegalArgumentException("Invalid pattern"); throw new IllegalArgumentException("Invalid pattern");
} }
String localeName = locale.toString();
// These locales don't use the Gregorian calendar // These locales don't use the Gregorian calendar
// See http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html // See http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
if (localeName.equals("ja_JP_JP") || localeName.startsWith("th_TH")) { // Also, the getEras() methods don't return the correct era names.
// N.B. Not safe to use toString() comparison because that changes between Java versions
if (locale.equals(JAPANESE_IMPERIAL)
|| (locale.getLanguage().equals("th") && locale.getCountry().equals("TH"))) {
collector.add(new SimpleDateFormatStrategy()); collector.add(new SimpleDateFormatStrategy());
strategies= collector.toArray(new Strategy[collector.size()]); strategies= collector.toArray(new Strategy[collector.size()]);
parsePattern= Pattern.compile("(.*+)"); parsePattern= Pattern.compile("(.*+)");

View File

@ -200,7 +200,7 @@ public class FastDateParserTest {
Calendar cal = Calendar.getInstance(tz); Calendar cal = Calendar.getInstance(tz);
for(int year : new int[]{2003, 1940, 1868, 1867, 0, -1940}) { for(int year : new int[]{2003, 1940, 1868, 1867, 0, -1940}) {
// http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html // http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
if (year < 1868 && locale.toString().equals("ja_JP_JP")) { if (year < 1868 && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
continue; // Japanese imperial calendar does not support eras before 1868 continue; // Japanese imperial calendar does not support eras before 1868
} }
cal.clear(); cal.clear();
@ -363,7 +363,7 @@ public class FastDateParserTest {
boolean failed = false; boolean failed = false;
for(Locale locale : Locale.getAvailableLocales()) { for(Locale locale : Locale.getAvailableLocales()) {
// ja_JP_JP cannot handle dates before 1868 properly // ja_JP_JP cannot handle dates before 1868 properly
if (eraBC && format.equals(SHORT_FORMAT) && locale.toString().equals("ja_JP_JP")) { if (eraBC && format.equals(SHORT_FORMAT) && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
continue; continue;
} }
SimpleDateFormat sdf = new SimpleDateFormat(format, locale); SimpleDateFormat sdf = new SimpleDateFormat(format, locale);