Java Code Examples for java.text.DateFormatSymbols#getInstance()

The following examples show how to use java.text.DateFormatSymbols#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtraDateStrings.java    From LGoodDatePicker with MIT License 6 votes vote down vote up
/**
 * getFormattingMonthName, This returns a "formatting version" month name for the specified
 * month, in the specified locale. In some languages, including Russian and Czech, the
 * standalone version of the month name is different from the version of the month name you
 * would use as part of a full date. (Is different from the formatting version).
 */
private static String getFormattingMonthName(Month month, Locale locale, boolean capitalize,
        boolean shortVersion) {
    // Get the "formatting version" of the month name.
    DateFormatSymbols dateSymbols = DateFormatSymbols.getInstance(locale);
    String monthName;
    if (shortVersion) {
        monthName = dateSymbols.getShortMonths()[month.getValue() - 1];
    } else {
        monthName = dateSymbols.getMonths()[month.getValue() - 1];
    }
    // If needed, capitalize the month name.
    if ((capitalize) && (monthName != null) && (monthName.length() > 0)) {
        monthName = monthName.substring(0, 1).toUpperCase(locale) + monthName.substring(1);
    }
    return monthName;
}
 
Example 2
Source File: CalendarText.java    From Time4A with Apache License 2.0 6 votes vote down vote up
@Override
public String[] months(
    String calendarType,
    Locale locale,
    TextWidth tw,
    OutputContext oc,
    boolean leapForm
) {

    DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);

    switch (tw) {
        case WIDE:
            return dfs.getMonths();
        case ABBREVIATED:
        case SHORT:
            return dfs.getShortMonths();
        case NARROW:
            return narrow(dfs.getShortMonths(), 12);
        default:
            throw new UnsupportedOperationException(tw.name());
    }

}
 
Example 3
Source File: SectionDecorator.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
private List<String> getAbbreviatedDayList() {
    List<String> list = new ArrayList<String>();
    ResourceLoader rl = new ResourceLoader();
    DateFormatSymbols dfs = DateFormatSymbols.getInstance(rl.getLocale());
    String[] daysOfWeek = dfs.getShortWeekdays();
    if(meeting.isMonday())
        list.add(daysOfWeek[Calendar.MONDAY]);
    if(meeting.isTuesday())
        list.add(daysOfWeek[Calendar.TUESDAY]);
    if(meeting.isWednesday())
        list.add(daysOfWeek[Calendar.WEDNESDAY]);
    if(meeting.isThursday())
        list.add(daysOfWeek[Calendar.THURSDAY]);
    if(meeting.isFriday())
        list.add(daysOfWeek[Calendar.FRIDAY]);
    if(meeting.isSaturday())
        list.add(daysOfWeek[Calendar.SATURDAY]);
    if(meeting.isSunday())
        list.add(daysOfWeek[Calendar.SUNDAY]);
    return list;
}
 
Example 4
Source File: Calendar.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 5
Source File: DateFormatSymbolsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_getInstanceLjava_util_Locale() {
    try {
        DateFormatSymbols.getInstance(null);
        fail();
    } catch (NullPointerException expected) {
    }

    assertEquals(new DateFormatSymbols(Locale.GERMANY), DateFormatSymbols.getInstance(Locale.GERMANY));

    Locale locale = new Locale("not exist language", "not exist country");
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    assertEquals(DateFormatSymbols.getInstance(Locale.ROOT), symbols);
}
 
Example 6
Source File: Calendar.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 7
Source File: Calendar.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<String,Integer>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 8
Source File: Cardumen_0096_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the short and long values displayed for a field
 * @param field The field of interest
 * @return A sorted array of the field key / value pairs
 */
KeyValue[] getDisplayNames(int field) {
    Integer fieldInt = Integer.valueOf(field);
    KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
    if(fieldKeyValues==null) {
        DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
        switch(field) {
        case Calendar.ERA:
            // DateFormatSymbols#getEras() only returns AD/BC or translations
            // It does not work for the Thai Buddhist or Japanese Imperial calendars.
            // see: https://issues.apache.org/jira/browse/TRINIDAD-2126
            Calendar c = Calendar.getInstance(locale);
            // N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
            String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
            String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
            fieldKeyValues= createKeyValues(longEras, shortEras);
            break;
        case Calendar.DAY_OF_WEEK:
            fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
            break;
        case Calendar.AM_PM:
            fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
            break;
        case Calendar.MONTH:
            fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
            break;
        default:
            throw new IllegalArgumentException("Invalid field value "+field);
        }
        KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
        if(prior!=null) {
            fieldKeyValues= prior;
        }
    }
    return fieldKeyValues;
}
 
Example 9
Source File: Calendar.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 10
Source File: DataFormatter1.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * Update formats when locale has been changed
 *
 * @param observable
 *            usually this is our own Observable instance
 * @param localeObj
 *            only reacts on Locale objects
 */
public void update(Observable observable, Object localeObj) {
    if (!(localeObj instanceof Locale))
        return;
    Locale newLocale = (Locale)localeObj;
    if (!localeIsAdapting || newLocale.equals(locale))
        return;

    locale = newLocale;

    dateSymbols = DateFormatSymbols.getInstance(locale);
    decimalSymbols = DecimalFormatSymbols.getInstance(locale);
    generalNumberFormat = new ExcelGeneralNumberFormat(locale);

    // taken from Date.toString()
    defaultDateformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", dateSymbols);
    defaultDateformat.setTimeZone(LocaleUtil.getUserTimeZone());

    // init built-in formats

    formats.clear();
    Format zipFormat = ZipPlusFourFormat.instance;
    addFormat("00000\\-0000", zipFormat);
    addFormat("00000-0000", zipFormat);

    Format phoneFormat = PhoneFormat.instance;
    // allow for format string variations
    addFormat("[<=9999999]###\\-####;\\(###\\)\\ ###\\-####", phoneFormat);
    addFormat("[<=9999999]###-####;(###) ###-####", phoneFormat);
    addFormat("###\\-####;\\(###\\)\\ ###\\-####", phoneFormat);
    addFormat("###-####;(###) ###-####", phoneFormat);

    Format ssnFormat = SSNFormat.instance;
    addFormat("000\\-00\\-0000", ssnFormat);
    addFormat("000-00-0000", ssnFormat);
}
 
Example 11
Source File: Lang_9_FastDateParser_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the short and long values displayed for a field
 * @param field The field of interest
 * @return A sorted array of the field key / value pairs
 */
KeyValue[] getDisplayNames(int field) {
    Integer fieldInt = Integer.valueOf(field);
    KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
    if(fieldKeyValues==null) {
        DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
        switch(field) {
        case Calendar.ERA:
            // DateFormatSymbols#getEras() only returns AD/BC or translations
            // It does not work for the Thai Buddhist or Japanese Imperial calendars.
            // see: https://issues.apache.org/jira/browse/TRINIDAD-2126
            Calendar c = Calendar.getInstance(locale);
            // N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
            String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
            String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
            fieldKeyValues= createKeyValues(longEras, shortEras);
            break;
        case Calendar.DAY_OF_WEEK:
            fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
            break;
        case Calendar.AM_PM:
            fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
            break;
        case Calendar.MONTH:
            fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
            break;
        default:
            throw new IllegalArgumentException("Invalid field value "+field);
        }
        KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
        if(prior!=null) {
            fieldKeyValues= prior;
        }
    }
    return fieldKeyValues;
}
 
Example 12
Source File: Calendar.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 13
Source File: Calendar.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 14
Source File: Cardumen_00154_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the short and long values displayed for a field
 * @param field The field of interest
 * @return A sorted array of the field key / value pairs
 */
KeyValue[] getDisplayNames(int field) {
    Integer fieldInt = Integer.valueOf(field);
    KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
    if (field < (pattern.length())) {
        DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
        switch(field) {
        case Calendar.ERA:
            // DateFormatSymbols#getEras() only returns AD/BC or translations
            // It does not work for the Thai Buddhist or Japanese Imperial calendars.
            // see: https://issues.apache.org/jira/browse/TRINIDAD-2126
            Calendar c = Calendar.getInstance(locale);
            // N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
            String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
            String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
            fieldKeyValues= createKeyValues(longEras, shortEras);
            break;
        case Calendar.DAY_OF_WEEK:
            fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
            break;
        case Calendar.AM_PM:
            fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
            break;
        case Calendar.MONTH:
            fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
            break;
        default:
            throw new IllegalArgumentException("Invalid field value "+field);
        }
        KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
        if(prior!=null) {
            fieldKeyValues= prior;
        }
    }
    return fieldKeyValues;
}
 
Example 15
Source File: Calendar.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private Map<String,Integer> getDisplayNamesImpl(int field, int style, Locale locale) {
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        Map<String,Integer> names = new HashMap<>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() == 0) {
                continue;
            }
            names.put(strings[i], i);
        }
        return names;
    }
    return null;
}
 
Example 16
Source File: Calendar.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the string representation of the calendar
 * <code>field</code> value in the given <code>style</code> and
 * <code>locale</code>.  If no string representation is
 * applicable, <code>null</code> is returned. This method calls
 * {@link Calendar#get(int) get(field)} to get the calendar
 * <code>field</code> value if the string representation is
 * applicable to the given calendar <code>field</code>.
 *
 * <p>For example, if this <code>Calendar</code> is a
 * <code>GregorianCalendar</code> and its date is 2005-01-01, then
 * the string representation of the {@link #MONTH} field would be
 * "January" in the long style in an English locale or "Jan" in
 * the short style. However, no string representation would be
 * available for the {@link #DAY_OF_MONTH} field, and this method
 * would return <code>null</code>.
 *
 * <p>The default implementation supports the calendar fields for
 * which a {@link DateFormatSymbols} has names in the given
 * <code>locale</code>.
 *
 * @param field
 *        the calendar field for which the string representation
 *        is returned
 * @param style
 *        the style applied to the string representation; one of {@link
 *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
 *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
 *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
 * @param locale
 *        the locale for the string representation
 *        (any calendar types specified by {@code locale} are ignored)
 * @return the string representation of the given
 *        {@code field} in the given {@code style}, or
 *        {@code null} if no string representation is
 *        applicable.
 * @exception IllegalArgumentException
 *        if {@code field} or {@code style} is invalid,
 *        or if this {@code Calendar} is non-lenient and any
 *        of the calendar fields have invalid values
 * @exception NullPointerException
 *        if {@code locale} is null
 * @since 1.6
 */
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                        ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

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

    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        int fieldValue = get(field);
        if (fieldValue < strings.length) {
            return strings[fieldValue];
        }
    }
    return null;
}
 
Example 17
Source File: Calendar.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the string representation of the calendar
 * <code>field</code> value in the given <code>style</code> and
 * <code>locale</code>.  If no string representation is
 * applicable, <code>null</code> is returned. This method calls
 * {@link Calendar#get(int) get(field)} to get the calendar
 * <code>field</code> value if the string representation is
 * applicable to the given calendar <code>field</code>.
 *
 * <p>For example, if this <code>Calendar</code> is a
 * <code>GregorianCalendar</code> and its date is 2005-01-01, then
 * the string representation of the {@link #MONTH} field would be
 * "January" in the long style in an English locale or "Jan" in
 * the short style. However, no string representation would be
 * available for the {@link #DAY_OF_MONTH} field, and this method
 * would return <code>null</code>.
 *
 * <p>The default implementation supports the calendar fields for
 * which a {@link DateFormatSymbols} has names in the given
 * <code>locale</code>.
 *
 * @param field
 *        the calendar field for which the string representation
 *        is returned
 * @param style
 *        the style applied to the string representation; one of {@link
 *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
 *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
 *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
 * @param locale
 *        the locale for the string representation
 *        (any calendar types specified by {@code locale} are ignored)
 * @return the string representation of the given
 *        {@code field} in the given {@code style}, or
 *        {@code null} if no string representation is
 *        applicable.
 * @exception IllegalArgumentException
 *        if {@code field} or {@code style} is invalid,
 *        or if this {@code Calendar} is non-lenient and any
 *        of the calendar fields have invalid values
 * @exception NullPointerException
 *        if {@code locale} is null
 * @since 1.6
 */
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                        ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    String calendarType = getCalendarType();
    int fieldValue = get(field);
    // the standalone and narrow styles are supported only through CalendarDataProviders.
    if (isStandaloneStyle(style) || isNarrowFormatStyle(style)) {
        String val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                field, fieldValue,
                                                                style, locale);
        // Perform fallback here to follow the CLDR rules
        if (val == null) {
            if (isNarrowFormatStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 toStandaloneStyle(style),
                                                                 locale);
            } else if (isStandaloneStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 getBaseStyle(style),
                                                                 locale);
            }
        }
        return val;
    }

    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        if (fieldValue < strings.length) {
            return strings[fieldValue];
        }
    }
    return null;
}
 
Example 18
Source File: Calendar.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Returns the string representation of the calendar
 * <code>field</code> value in the given <code>style</code> and
 * <code>locale</code>.  If no string representation is
 * applicable, <code>null</code> is returned. This method calls
 * {@link Calendar#get(int) get(field)} to get the calendar
 * <code>field</code> value if the string representation is
 * applicable to the given calendar <code>field</code>.
 *
 * <p>For example, if this <code>Calendar</code> is a
 * <code>GregorianCalendar</code> and its date is 2005-01-01, then
 * the string representation of the {@link #MONTH} field would be
 * "January" in the long style in an English locale or "Jan" in
 * the short style. However, no string representation would be
 * available for the {@link #DAY_OF_MONTH} field, and this method
 * would return <code>null</code>.
 *
 * <p>The default implementation supports the calendar fields for
 * which a {@link DateFormatSymbols} has names in the given
 * <code>locale</code>.
 *
 * @param field
 *        the calendar field for which the string representation
 *        is returned
 * @param style
 *        the style applied to the string representation; one of {@link
 *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
 *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
 *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
 * @param locale
 *        the locale for the string representation
 *        (any calendar types specified by {@code locale} are ignored)
 * @return the string representation of the given
 *        {@code field} in the given {@code style}, or
 *        {@code null} if no string representation is
 *        applicable.
 * @exception IllegalArgumentException
 *        if {@code field} or {@code style} is invalid,
 *        or if this {@code Calendar} is non-lenient and any
 *        of the calendar fields have invalid values
 * @exception NullPointerException
 *        if {@code locale} is null
 * @since 1.6
 */
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                        ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    String calendarType = getCalendarType();
    int fieldValue = get(field);
    // the standalone and narrow styles are supported only through CalendarDataProviders.
    if (isStandaloneStyle(style) || isNarrowFormatStyle(style)) {
        String val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                field, fieldValue,
                                                                style, locale);
        // Perform fallback here to follow the CLDR rules
        if (val == null) {
            if (isNarrowFormatStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 toStandaloneStyle(style),
                                                                 locale);
            } else if (isStandaloneStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 getBaseStyle(style),
                                                                 locale);
            }
        }
        return val;
    }

    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        if (fieldValue < strings.length) {
            return strings[fieldValue];
        }
    }
    return null;
}
 
Example 19
Source File: Calendar.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the string representation of the calendar
 * <code>field</code> value in the given <code>style</code> and
 * <code>locale</code>.  If no string representation is
 * applicable, <code>null</code> is returned. This method calls
 * {@link Calendar#get(int) get(field)} to get the calendar
 * <code>field</code> value if the string representation is
 * applicable to the given calendar <code>field</code>.
 *
 * <p>For example, if this <code>Calendar</code> is a
 * <code>GregorianCalendar</code> and its date is 2005-01-01, then
 * the string representation of the {@link #MONTH} field would be
 * "January" in the long style in an English locale or "Jan" in
 * the short style. However, no string representation would be
 * available for the {@link #DAY_OF_MONTH} field, and this method
 * would return <code>null</code>.
 *
 * <p>The default implementation supports the calendar fields for
 * which a {@link DateFormatSymbols} has names in the given
 * <code>locale</code>.
 *
 * @param field
 *        the calendar field for which the string representation
 *        is returned
 * @param style
 *        the style applied to the string representation; one of {@link
 *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
 *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
 *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
 * @param locale
 *        the locale for the string representation
 *        (any calendar types specified by {@code locale} are ignored)
 * @return the string representation of the given
 *        {@code field} in the given {@code style}, or
 *        {@code null} if no string representation is
 *        applicable.
 * @exception IllegalArgumentException
 *        if {@code field} or {@code style} is invalid,
 *        or if this {@code Calendar} is non-lenient and any
 *        of the calendar fields have invalid values
 * @exception NullPointerException
 *        if {@code locale} is null
 * @since 1.6
 */
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                        ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    String calendarType = getCalendarType();
    int fieldValue = get(field);
    // the standalone and narrow styles are supported only through CalendarDataProviders.
    if (isStandaloneStyle(style) || isNarrowFormatStyle(style)) {
        String val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                field, fieldValue,
                                                                style, locale);
        // Perform fallback here to follow the CLDR rules
        if (val == null) {
            if (isNarrowFormatStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 toStandaloneStyle(style),
                                                                 locale);
            } else if (isStandaloneStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 getBaseStyle(style),
                                                                 locale);
            }
        }
        return val;
    }

    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        if (fieldValue < strings.length) {
            return strings[fieldValue];
        }
    }
    return null;
}
 
Example 20
Source File: Calendar.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the string representation of the calendar
 * <code>field</code> value in the given <code>style</code> and
 * <code>locale</code>.  If no string representation is
 * applicable, <code>null</code> is returned. This method calls
 * {@link Calendar#get(int) get(field)} to get the calendar
 * <code>field</code> value if the string representation is
 * applicable to the given calendar <code>field</code>.
 *
 * <p>For example, if this <code>Calendar</code> is a
 * <code>GregorianCalendar</code> and its date is 2005-01-01, then
 * the string representation of the {@link #MONTH} field would be
 * "January" in the long style in an English locale or "Jan" in
 * the short style. However, no string representation would be
 * available for the {@link #DAY_OF_MONTH} field, and this method
 * would return <code>null</code>.
 *
 * <p>The default implementation supports the calendar fields for
 * which a {@link DateFormatSymbols} has names in the given
 * <code>locale</code>.
 *
 * @param field
 *        the calendar field for which the string representation
 *        is returned
 * @param style
 *        the style applied to the string representation; one of {@link
 *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
 *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
 *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
 * @param locale
 *        the locale for the string representation
 *        (any calendar types specified by {@code locale} are ignored)
 * @return the string representation of the given
 *        {@code field} in the given {@code style}, or
 *        {@code null} if no string representation is
 *        applicable.
 * @exception IllegalArgumentException
 *        if {@code field} or {@code style} is invalid,
 *        or if this {@code Calendar} is non-lenient and any
 *        of the calendar fields have invalid values
 * @exception NullPointerException
 *        if {@code locale} is null
 * @since 1.6
 */
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                        ERA_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    String calendarType = getCalendarType();
    int fieldValue = get(field);
    // the standalone/narrow styles and short era are supported only through
    // CalendarNameProviders.
    if (isStandaloneStyle(style) || isNarrowFormatStyle(style) ||
        field == ERA && (style & SHORT) == SHORT) {
        String val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                field, fieldValue,
                                                                style, locale);
        // Perform fallback here to follow the CLDR rules
        if (val == null) {
            if (isNarrowFormatStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 toStandaloneStyle(style),
                                                                 locale);
            } else if (isStandaloneStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType,
                                                                 field, fieldValue,
                                                                 getBaseStyle(style),
                                                                 locale);
            }
        }
        return val;
    }

    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        if (fieldValue < strings.length) {
            return strings[fieldValue];
        }
    }
    return null;
}