diff options
-rw-r--r-- | luni/src/main/java/java/text/SimpleDateFormat.java | 35 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/text/SimpleDateFormatTest.java | 7 |
2 files changed, 31 insertions, 11 deletions
diff --git a/luni/src/main/java/java/text/SimpleDateFormat.java b/luni/src/main/java/java/text/SimpleDateFormat.java index 04287ae..ea90bbb 100644 --- a/luni/src/main/java/java/text/SimpleDateFormat.java +++ b/luni/src/main/java/java/text/SimpleDateFormat.java @@ -1130,22 +1130,35 @@ public class SimpleDateFormat extends DateFormat { return position.getIndex(); } - private int parseText(String string, int offset, String[] text, int field) { - int found = -1; - for (int i = 0; i < text.length; i++) { - if (text[i].isEmpty()) { + private int parseText(String string, int offset, String[] options, int field) { + // We search for the longest match, in case some entries are substrings of others. + int bestIndex = -1; + int bestLength = -1; + for (int i = 0; i < options.length; ++i) { + String option = options[i]; + int optionLength = option.length(); + if (optionLength == 0) { continue; } - if (string.regionMatches(true, offset, text[i], 0, text[i].length())) { - // Search for the longest match, in case some fields are subsets - if (found == -1 || text[i].length() > text[found].length()) { - found = i; + if (string.regionMatches(true, offset, option, 0, optionLength)) { + if (bestIndex == -1 || optionLength > bestLength) { + bestIndex = i; + bestLength = optionLength; + } + } else if (option.charAt(optionLength - 1) == '.') { + // If CLDR has abbreviated forms like "Aug.", we should accept "Aug" too. + // https://code.google.com/p/android/issues/detail?id=59383 + if (string.regionMatches(true, offset, option, 0, optionLength - 1)) { + if (bestIndex == -1 || optionLength - 1 > bestLength) { + bestIndex = i; + bestLength = optionLength - 1; + } } } } - if (found != -1) { - calendar.set(field, found); - return offset + text[found].length(); + if (bestIndex != -1) { + calendar.set(field, bestIndex); + return offset + bestLength; } return -offset - 1; } diff --git a/luni/src/test/java/libcore/java/text/SimpleDateFormatTest.java b/luni/src/test/java/libcore/java/text/SimpleDateFormatTest.java index 21d7302..2813cee 100644 --- a/luni/src/test/java/libcore/java/text/SimpleDateFormatTest.java +++ b/luni/src/test/java/libcore/java/text/SimpleDateFormatTest.java @@ -346,4 +346,11 @@ public class SimpleDateFormatTest extends junit.framework.TestCase { Date d2 = sdf.parse(formatted); assertEquals(d, d2); } + + public void test_59383() throws Exception { + SimpleDateFormat sdf = new SimpleDateFormat("d. MMM yyyy H:mm", Locale.GERMAN); + sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); + assertEquals(1376927400000L, sdf.parse("19. Aug 2013 8:50").getTime()); + assertEquals(1376927400000L, sdf.parse("19. Aug. 2013 8:50").getTime()); + } } |