< prev index next >

src/share/classes/java/util/Calendar.java

Print this page




2066      *        the locale for the string representation
2067      *        (any calendar types specified by {@code locale} are ignored)
2068      * @return the string representation of the given
2069      *        {@code field} in the given {@code style}, or
2070      *        {@code null} if no string representation is
2071      *        applicable.
2072      * @exception IllegalArgumentException
2073      *        if {@code field} or {@code style} is invalid,
2074      *        or if this {@code Calendar} is non-lenient and any
2075      *        of the calendar fields have invalid values
2076      * @exception NullPointerException
2077      *        if {@code locale} is null
2078      * @since 1.6
2079      */
2080     public String getDisplayName(int field, int style, Locale locale) {
2081         if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
2082                             ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
2083             return null;
2084         }
2085 


2086         // the standalone and narrow styles are supported only through CalendarDataProviders.
2087         if (isStandaloneStyle(style) || isNarrowStyle(style)) {
2088             return CalendarDataUtility.retrieveFieldValueName(getCalendarType(),
2089                                                               field, get(field),
2090                                                               style, locale);















2091         }
2092 
2093         DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
2094         String[] strings = getFieldStrings(field, style, symbols);
2095         if (strings != null) {
2096             int fieldValue = get(field);
2097             if (fieldValue < strings.length) {
2098                 return strings[fieldValue];
2099             }
2100         }
2101         return null;
2102     }
2103 
2104     /**
2105      * Returns a {@code Map} containing all names of the calendar
2106      * {@code field} in the given {@code style} and
2107      * {@code locale} and their corresponding field values. For
2108      * example, if this {@code Calendar} is a {@link
2109      * GregorianCalendar}, the returned map would contain "Jan" to
2110      * {@link #JANUARY}, "Feb" to {@link #FEBRUARY}, and so on, in the
2111      * {@linkplain #SHORT short} style in an English locale.
2112      *
2113      * <p>Narrow names may not be unique due to use of single characters,
2114      * such as "S" for Sunday and Saturday. In that case narrow names are not
2115      * included in the returned {@code Map}.
2116      *


2138      *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}
2139      * @param locale
2140      *        the locale for the display names
2141      * @return a {@code Map} containing all display names in
2142      *        {@code style} and {@code locale} and their
2143      *        field values, or {@code null} if no display names
2144      *        are defined for {@code field}
2145      * @exception IllegalArgumentException
2146      *        if {@code field} or {@code style} is invalid,
2147      *        or if this {@code Calendar} is non-lenient and any
2148      *        of the calendar fields have invalid values
2149      * @exception NullPointerException
2150      *        if {@code locale} is null
2151      * @since 1.6
2152      */
2153     public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
2154         if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
2155                                     ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
2156             return null;
2157         }
2158         if (style == ALL_STYLES || isStandaloneStyle(style)) {
2159             return CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);













2160         }
2161         // SHORT, LONG, or NARROW



2162         return getDisplayNamesImpl(field, style, locale);
2163     }
2164 
2165     private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
2166         DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
2167         String[] strings = getFieldStrings(field, style, symbols);
2168         if (strings != null) {
2169             Map<String,Integer> names = new HashMap<>();
2170             for (int i = 0; i < strings.length; i++) {
2171                 if (strings[i].length() == 0) {
2172                     continue;
2173                 }
2174                 names.put(strings[i], i);
2175             }
2176             return names;
2177         }
2178         return null;
2179     }
2180 
2181     boolean checkDisplayNameParams(int field, int style, int minStyle, int maxStyle,


2527         if (stamp[SECOND] != UNSET) {
2528             fieldMask |= SECOND_MASK;
2529         }
2530         if (stamp[MILLISECOND] != UNSET) {
2531             fieldMask |= MILLISECOND_MASK;
2532         }
2533         if (stamp[ZONE_OFFSET] >= MINIMUM_USER_STAMP) {
2534                 fieldMask |= ZONE_OFFSET_MASK;
2535         }
2536         if (stamp[DST_OFFSET] >= MINIMUM_USER_STAMP) {
2537             fieldMask |= DST_OFFSET_MASK;
2538         }
2539 
2540         return fieldMask;
2541     }
2542 
2543     int getBaseStyle(int style) {
2544         return style & ~STANDALONE_MASK;
2545     }
2546 
2547     boolean isStandaloneStyle(int style) {




2548         return (style & STANDALONE_MASK) != 0;
2549     }
2550 
2551     boolean isNarrowStyle(int style) {
2552         return style == NARROW_FORMAT || style == NARROW_STANDALONE;




2553     }
2554 
2555     /**
2556      * Returns the pseudo-time-stamp for two fields, given their
2557      * individual pseudo-time-stamps.  If either of the fields
2558      * is unset, then the aggregate is unset.  Otherwise, the
2559      * aggregate is the later of the two stamps.
2560      */
2561     private static int aggregateStamp(int stamp_a, int stamp_b) {
2562         if (stamp_a == UNSET || stamp_b == UNSET) {
2563             return UNSET;
2564         }
2565         return (stamp_a > stamp_b) ? stamp_a : stamp_b;
2566     }
2567 
2568     /**
2569      * Returns an unmodifiable {@code Set} containing all calendar types
2570      * supported by {@code Calendar} in the runtime environment. The available
2571      * calendar types can be used for the <a
2572      * href="Locale.html#def_locale_extension">Unicode locale extensions</a>.




2066      *        the locale for the string representation
2067      *        (any calendar types specified by {@code locale} are ignored)
2068      * @return the string representation of the given
2069      *        {@code field} in the given {@code style}, or
2070      *        {@code null} if no string representation is
2071      *        applicable.
2072      * @exception IllegalArgumentException
2073      *        if {@code field} or {@code style} is invalid,
2074      *        or if this {@code Calendar} is non-lenient and any
2075      *        of the calendar fields have invalid values
2076      * @exception NullPointerException
2077      *        if {@code locale} is null
2078      * @since 1.6
2079      */
2080     public String getDisplayName(int field, int style, Locale locale) {
2081         if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
2082                             ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
2083             return null;
2084         }
2085 
2086         String calendarType = getCalendarType();
2087         int fieldValue = get(field);
2088         // the standalone and narrow styles are supported only through CalendarDataProviders.
2089         if (isStandaloneStyle(style) || isNarrowFormatStyle(style)) {
2090             String val = CalendarDataUtility.retrieveFieldValueName(calendarType,
2091                                                                     field, fieldValue,
2092                                                                     style, locale);
2093             // Perform fallback here to follow the CLDR rules
2094             if (val == null) {
2095                 if (isNarrowFormatStyle(style)) {
2096                     val = CalendarDataUtility.retrieveFieldValueName(calendarType,
2097                                                                      field, fieldValue,
2098                                                                      toStandaloneStyle(style),
2099                                                                      locale);
2100                 } else if (isStandaloneStyle(style)) {
2101                     val = CalendarDataUtility.retrieveFieldValueName(calendarType,
2102                                                                      field, fieldValue,
2103                                                                      getBaseStyle(style),
2104                                                                      locale);
2105                 }
2106             }
2107             return val;
2108         }
2109 
2110         DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
2111         String[] strings = getFieldStrings(field, style, symbols);
2112         if (strings != null) {

2113             if (fieldValue < strings.length) {
2114                 return strings[fieldValue];
2115             }
2116         }
2117         return null;
2118     }
2119 
2120     /**
2121      * Returns a {@code Map} containing all names of the calendar
2122      * {@code field} in the given {@code style} and
2123      * {@code locale} and their corresponding field values. For
2124      * example, if this {@code Calendar} is a {@link
2125      * GregorianCalendar}, the returned map would contain "Jan" to
2126      * {@link #JANUARY}, "Feb" to {@link #FEBRUARY}, and so on, in the
2127      * {@linkplain #SHORT short} style in an English locale.
2128      *
2129      * <p>Narrow names may not be unique due to use of single characters,
2130      * such as "S" for Sunday and Saturday. In that case narrow names are not
2131      * included in the returned {@code Map}.
2132      *


2154      *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}
2155      * @param locale
2156      *        the locale for the display names
2157      * @return a {@code Map} containing all display names in
2158      *        {@code style} and {@code locale} and their
2159      *        field values, or {@code null} if no display names
2160      *        are defined for {@code field}
2161      * @exception IllegalArgumentException
2162      *        if {@code field} or {@code style} is invalid,
2163      *        or if this {@code Calendar} is non-lenient and any
2164      *        of the calendar fields have invalid values
2165      * @exception NullPointerException
2166      *        if {@code locale} is null
2167      * @since 1.6
2168      */
2169     public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
2170         if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
2171                                     ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
2172             return null;
2173         }
2174 
2175         String calendarType = getCalendarType();
2176         if (style == ALL_STYLES || isStandaloneStyle(style) || isNarrowFormatStyle(style)) {
2177             Map<String, Integer> map;
2178             map = CalendarDataUtility.retrieveFieldValueNames(calendarType, field, style, locale);
2179 
2180             // Perform fallback here to follow the CLDR rules
2181             if (map == null) {
2182                 if (isNarrowFormatStyle(style)) {
2183                     map = CalendarDataUtility.retrieveFieldValueNames(calendarType, field,
2184                                                                       toStandaloneStyle(style), locale);
2185                 } else if (style != ALL_STYLES) {
2186                     map = CalendarDataUtility.retrieveFieldValueNames(calendarType, field,
2187                                                                       getBaseStyle(style), locale);
2188                 }
2189             }
2190             return map;
2191         }
2192 
2193         // SHORT or LONG
2194         return getDisplayNamesImpl(field, style, locale);
2195     }
2196 
2197     private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
2198         DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
2199         String[] strings = getFieldStrings(field, style, symbols);
2200         if (strings != null) {
2201             Map<String,Integer> names = new HashMap<>();
2202             for (int i = 0; i < strings.length; i++) {
2203                 if (strings[i].length() == 0) {
2204                     continue;
2205                 }
2206                 names.put(strings[i], i);
2207             }
2208             return names;
2209         }
2210         return null;
2211     }
2212 
2213     boolean checkDisplayNameParams(int field, int style, int minStyle, int maxStyle,


2559         if (stamp[SECOND] != UNSET) {
2560             fieldMask |= SECOND_MASK;
2561         }
2562         if (stamp[MILLISECOND] != UNSET) {
2563             fieldMask |= MILLISECOND_MASK;
2564         }
2565         if (stamp[ZONE_OFFSET] >= MINIMUM_USER_STAMP) {
2566                 fieldMask |= ZONE_OFFSET_MASK;
2567         }
2568         if (stamp[DST_OFFSET] >= MINIMUM_USER_STAMP) {
2569             fieldMask |= DST_OFFSET_MASK;
2570         }
2571 
2572         return fieldMask;
2573     }
2574 
2575     int getBaseStyle(int style) {
2576         return style & ~STANDALONE_MASK;
2577     }
2578 
2579     private int toStandaloneStyle(int style) {
2580         return style | STANDALONE_MASK;
2581     }
2582 
2583     private boolean isStandaloneStyle(int style) {
2584         return (style & STANDALONE_MASK) != 0;
2585     }
2586 
2587     private boolean isNarrowStyle(int style) {
2588         return style == NARROW_FORMAT || style == NARROW_STANDALONE;
2589     }
2590 
2591     private boolean isNarrowFormatStyle(int style) {
2592         return style == NARROW_FORMAT;
2593     }
2594 
2595     /**
2596      * Returns the pseudo-time-stamp for two fields, given their
2597      * individual pseudo-time-stamps.  If either of the fields
2598      * is unset, then the aggregate is unset.  Otherwise, the
2599      * aggregate is the later of the two stamps.
2600      */
2601     private static int aggregateStamp(int stamp_a, int stamp_b) {
2602         if (stamp_a == UNSET || stamp_b == UNSET) {
2603             return UNSET;
2604         }
2605         return (stamp_a > stamp_b) ? stamp_a : stamp_b;
2606     }
2607 
2608     /**
2609      * Returns an unmodifiable {@code Set} containing all calendar types
2610      * supported by {@code Calendar} in the runtime environment. The available
2611      * calendar types can be used for the <a
2612      * href="Locale.html#def_locale_extension">Unicode locale extensions</a>.


< prev index next >