diff --git a/src/share/classes/java/util/Calendar.java b/src/share/classes/java/util/Calendar.java index 03342411d0b826956cb0ef5b35666bd2eab862d4..3efdc0f513d6471753744536679123246955e360 100644 --- a/src/share/classes/java/util/Calendar.java +++ b/src/share/classes/java/util/Calendar.java @@ -2083,17 +2083,33 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable map; + map = CalendarDataUtility.retrieveFieldValueNames(calendarType, field, style, locale); + + // Perform fallback here to follow the CLDR rules + if (map == null) { + if (isNarrowFormatStyle(style)) { + map = CalendarDataUtility.retrieveFieldValueNames(calendarType, field, + toStandaloneStyle(style), locale); + } else if (style != ALL_STYLES) { + map = CalendarDataUtility.retrieveFieldValueNames(calendarType, field, + getBaseStyle(style), locale); + } + } + return map; } - // SHORT, LONG, or NARROW + + // SHORT or LONG return getDisplayNamesImpl(field, style, locale); } @@ -2544,14 +2576,22 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable " + el + ", " + + formats[1] + " -> " + em); + } + } + } + + // Test Calendar.getDisplayName() and .getDisplayNames(). + for (Locale locale : LOCALES) { + testDisplayNames(locale, LONG_FORMAT, LONG_STANDALONE); + testDisplayNames(locale, SHORT_FORMAT, SHORT_STANDALONE); + testDisplayNames(locale, NARROW_FORMAT, NARROW_STANDALONE); + } + + if (errors > 0) { + throw new RuntimeException("Failed"); + } + } + + private static void testDisplayNames(Locale locale, int formatStyle, int standaloneStyle) { + Map map = new HashMap<>(); + for (int month = JANUARY; month <= DECEMBER; month++) { + Calendar cal = new GregorianCalendar(2015, month, 1); + String format = cal.getDisplayName(MONTH, formatStyle, locale); + String standalone = cal.getDisplayName(MONTH, standaloneStyle, locale); + if (!format.equals(standalone)) { + System.err.println("Calendar.getDisplayName: " + (month+1) + + ", locale=" + locale + + ", format=" + format + ", standalone=" + standalone); + errors++; + } + if (standalone != null) { + map.put(standalone, month); + } + } + if (formatStyle == NARROW_FORMAT) { + // Narrow styles don't support unique names. + // (e.g., "J" for JANUARY, JUNE, and JULY) + return; + } + Calendar cal = new GregorianCalendar(2015, JANUARY, 1); + Map mapStandalone = cal.getDisplayNames(MONTH, standaloneStyle, locale); + if (!map.equals(mapStandalone)) { + System.err.printf("Calendar.getDisplayNames: locale=%s%n map=%s%n mapStandalone=%s%n", + locale, map, mapStandalone); + errors++; + } + Map mapAll = cal.getDisplayNames(MONTH, ALL_STYLES, locale); + if (!mapAll.entrySet().containsAll(map.entrySet())) { + System.err.printf("Calendar.getDisplayNames: locale=%s%n map=%s%n mapAll=%s%n", + locale, map, mapAll); + errors++; + } + } +} diff --git a/test/java/util/Calendar/NarrowNamesTest.java b/test/java/util/Calendar/NarrowNamesTest.java index 7338792abd35a3c00a093cd6f7384c717b7bc7e3..1df04763341475d85e848a21f789dd228a85ab77 100644 --- a/test/java/util/Calendar/NarrowNamesTest.java +++ b/test/java/util/Calendar/NarrowNamesTest.java @@ -86,7 +86,19 @@ public class NarrowNamesTest { "\u6728", "\u91d1", "\u571f"); - testMap(THTH, MONTH, NARROW_FORMAT); // expect null + testMap(THTH, MONTH, NARROW_FORMAT, + "\u0e21.\u0e04.", + "\u0e01.\u0e1e.", + "\u0e21\u0e35.\u0e04.", + "\u0e40\u0e21.\u0e22.", + "\u0e1e.\u0e04.", + "\u0e21\u0e34.\u0e22", // no last dot + "\u0e01.\u0e04.", + "\u0e2a.\u0e04.", + "\u0e01.\u0e22.", + "\u0e15.\u0e04.", + "\u0e1e.\u0e22.", + "\u0e18.\u0e04."); testMap(THTH, MONTH, NARROW_STANDALONE, "\u0e21.\u0e04.", "\u0e01.\u0e1e.", @@ -146,7 +158,7 @@ public class NarrowNamesTest { Calendar cal = Calendar.getInstance(locale); Map got = cal.getDisplayNames(field, style, locale); if (!(expectedMap == null && got == null) - && !expectedMap.equals(got)) { + && !(expectedMap != null && expectedMap.equals(got))) { System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n", locale, field, style, expectedMap, got); errors++;