Java Code Examples for sun.util.locale.provider.CalendarDataUtility#retrieveFieldValueName()

The following examples show how to use sun.util.locale.provider.CalendarDataUtility#retrieveFieldValueName() . 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: JapaneseImperialCalendar.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 2
Source File: JapaneseImperialCalendar.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 3
Source File: JapaneseImperialCalendar.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 4
Source File: JapaneseImperialCalendar.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 5
Source File: JapaneseImperialCalendar.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 6
Source File: JapaneseImperialCalendar.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 7
Source File: JapaneseImperialCalendar.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null, then
    // try to get its name or abbreviation from the Era instance.
    if (name == null && field == ERA && fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 8
Source File: BuddhistCalendar.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 9
Source File: BuddhistCalendar.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 10
Source File: BuddhistCalendar.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 11
Source File: BuddhistCalendar.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 12
Source File: BuddhistCalendar.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 13
Source File: BuddhistCalendar.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 14
Source File: BuddhistCalendar.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayName(field, style, locale);
    }

    return CalendarDataUtility.retrieveFieldValueName("buddhist", field, get(field), style, locale);
}
 
Example 15
Source File: JapaneseImperialCalendar.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }

    int fieldValue = get(field);

    // "GanNen" is supported only in the LONG style.
    if (field == YEAR
        && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
        return null;
    }

    String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
                                                             fieldValue, style, locale);
    // If the ERA value is null or empty, then
    // try to get its name or abbreviation from the Era instance.
    if ((name == null || name.isEmpty()) &&
            field == ERA &&
            fieldValue < eras.length) {
        Era era = eras[fieldValue];
        name = (style == SHORT) ? era.getAbbreviation() : era.getName();
    }
    return name;
}
 
Example 16
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 17
Source File: Calendar.java    From jdk8u-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 18
Source File: Calendar.java    From openjdk-jdk8u-backup 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 19
Source File: Calendar.java    From dragonwell8_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 Java8CN 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;
}