sun.util.locale.provider.CalendarDataUtility Java Examples

The following examples show how to use sun.util.locale.provider.CalendarDataUtility. 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: CLDRCalendarDataProviderImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the requested integer value for the locale.
 * Each resource consists of the following:
 *
 *    (n: cc1 cc2 ... ccx;)*
 *
 * where 'n' is the integer for the following region codes, terminated by
 * a ';'.
 *
 */
private static int findValue(String key, Locale locale) {
    Map<String, Integer> map = CalendarDataUtility.FIRST_DAY_OF_WEEK.equals(key) ?
        firstDay : minDays;
    String region = locale.getCountry();

    if (region.isEmpty()) {
        // Use "US" as default
        region = "US";
    }

    Integer val = map.get(region);
    if (val == null) {
        String valStr =
            LocaleProviderAdapter.forType(Type.CLDR).getLocaleResources(Locale.ROOT)
               .getCalendarData(key);
        val = retrieveInteger(valStr, region)
            .orElse(retrieveInteger(valStr, "001").orElse(0));
        map.putIfAbsent(region, val);
    }

    return val;
}
 
Example #2
Source File: JapaneseImperialCalendar.java    From jdk1.8-source-analysis 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 #3
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 #4
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 #5
Source File: JapaneseImperialCalendar.java    From hottub 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 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 #7
Source File: JapaneseImperialCalendar.java    From openjdk-8 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: DateTimeTextProvider.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #9
Source File: DateTimeTextProvider.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #10
Source File: DateTimeTextProvider.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #11
Source File: DateTimeTextProvider.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #12
Source File: WeekFields.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public String getDisplayName(Locale locale) {
    Objects.requireNonNull(locale, "locale");
    if (rangeUnit == YEARS) {  // only have values for week-of-year
        LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
                .getLocaleResources(
                    CalendarDataUtility.findRegionOverride(locale));
        ResourceBundle rb = lr.getJavaTimeFormatData();
        return rb.containsKey("field.week") ? rb.getString("field.week") : name;
    }
    return name;
}
 
Example #13
Source File: IsoFields.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public String getDisplayName(Locale locale) {
    Objects.requireNonNull(locale, "locale");
    LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
                                .getLocaleResources(
                                    CalendarDataUtility
                                        .findRegionOverride(locale));
    ResourceBundle rb = lr.getJavaTimeFormatData();
    return rb.containsKey("field.week") ? rb.getString("field.week") : toString();
}
 
Example #14
Source File: Bug8007038.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void checkValueRange(String calType, int field, int value, int style, Locale l, boolean isNonNull) {
    String ret = CalendarDataUtility.retrieveJavaTimeFieldValueName(calType, field, value, style, l);
    System.out.print("retrieveFieldValueName("+calType+", "+field+", "+value+", "+style+", "+l+")");
    if ((ret != null) == isNonNull) {
        System.out.println(" returned "+ret);
    } else {
        throw new RuntimeException("The call returned "+ret);
    }
}
 
Example #15
Source File: BuddhistCalendar.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayNames(field, style, locale);
    }
    return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale);
}
 
Example #16
Source File: Calendar.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
 * They are used to figure out the week count for a specific date for
 * a given locale. These must be set when a Calendar is constructed.
 * @param desiredLocale the given locale.
 */
private void setWeekCountData(Locale desiredLocale)
{
    /* try to get the Locale data from the cache */
    int[] data = cachedLocaleData.get(desiredLocale);
    if (data == null) {  /* cache miss */
        data = new int[2];
        data[0] = CalendarDataUtility.retrieveFirstDayOfWeek(desiredLocale);
        data[1] = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(desiredLocale);
        cachedLocaleData.putIfAbsent(desiredLocale, data);
    }
    firstDayOfWeek = data[0];
    minimalDaysInFirstWeek = data[1];
}
 
Example #17
Source File: BuddhistCalendar.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayNames(field, style, locale);
    }
    return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale);
}
 
Example #18
Source File: BuddhistCalendar.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayNames(field, style, locale);
    }
    return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale);
}
 
Example #19
Source File: Bug8007038.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkValueRange(String calType, int field, int value, int style, Locale l, boolean isNonNull) {
    String ret = CalendarDataUtility.retrieveJavaTimeFieldValueName(calType, field, value, style, l);
    System.out.print("retrieveFieldValueName("+calType+", "+field+", "+value+", "+style+", "+l+")");
    if ((ret != null) == isNonNull) {
        System.out.println(" returned "+ret);
    } else {
        throw new RuntimeException("The call returned "+ret);
    }
}
 
Example #20
Source File: JapaneseImperialCalendar.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }
    Map<String, Integer> names;
    names = CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);
    // If strings[] has fewer than eras[], get more names from eras[].
    if (names != null) {
        if (field == ERA) {
            int size = names.size();
            if (style == ALL_STYLES) {
                Set<Integer> values = new HashSet<>();
                // count unique era values
                for (String key : names.keySet()) {
                    values.add(names.get(key));
                }
                size = values.size();
            }
            if (size < eras.length) {
                int baseStyle = getBaseStyle(style);
                for (int i = size; i < eras.length; i++) {
                    Era era = eras[i];
                    if (baseStyle == ALL_STYLES || baseStyle == SHORT
                            || baseStyle == NARROW_FORMAT) {
                        names.put(era.getAbbreviation(), i);
                    }
                    if (baseStyle == ALL_STYLES || baseStyle == LONG) {
                        names.put(era.getName(), i);
                    }
                }
            }
        }
    }
    return names;
}
 
Example #21
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 #22
Source File: Calendar.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
 * They are used to figure out the week count for a specific date for
 * a given locale. These must be set when a Calendar is constructed.
 * @param desiredLocale the given locale.
 */
private void setWeekCountData(Locale desiredLocale)
{
    /* try to get the Locale data from the cache */
    int[] data = cachedLocaleData.get(desiredLocale);
    if (data == null) {  /* cache miss */
        data = new int[2];
        data[0] = CalendarDataUtility.retrieveFirstDayOfWeek(desiredLocale);
        data[1] = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(desiredLocale);
        cachedLocaleData.putIfAbsent(desiredLocale, data);
    }
    firstDayOfWeek = data[0];
    minimalDaysInFirstWeek = data[1];
}
 
Example #23
Source File: BuddhistCalendar.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (field != ERA) {
        return super.getDisplayNames(field, style, locale);
    }
    return CalendarDataUtility.retrieveFieldValueNames("buddhist", field, style, locale);
}
 
Example #24
Source File: DateTimeTextProvider.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #25
Source File: JapaneseImperialCalendar.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }
    Map<String, Integer> names;
    names = CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);
    // If strings[] has fewer than eras[], get more names from eras[].
    if (names != null) {
        if (field == ERA) {
            int size = names.size();
            if (style == ALL_STYLES) {
                Set<Integer> values = new HashSet<>();
                // count unique era values
                for (String key : names.keySet()) {
                    values.add(names.get(key));
                }
                size = values.size();
            }
            if (size < eras.length) {
                int baseStyle = getBaseStyle(style);
                for (int i = size; i < eras.length; i++) {
                    Era era = eras[i];
                    if (baseStyle == ALL_STYLES || baseStyle == SHORT
                            || baseStyle == NARROW_FORMAT) {
                        names.put(era.getAbbreviation(), i);
                    }
                    if (baseStyle == ALL_STYLES || baseStyle == LONG) {
                        names.put(era.getName(), i);
                    }
                }
            }
        }
    }
    return names;
}
 
Example #26
Source File: JapaneseImperialCalendar.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
                                ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
        return null;
    }
    Map<String, Integer> names;
    names = CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);
    // If strings[] has fewer than eras[], get more names from eras[].
    if (names != null) {
        if (field == ERA) {
            int size = names.size();
            if (style == ALL_STYLES) {
                Set<Integer> values = new HashSet<>();
                // count unique era values
                for (String key : names.keySet()) {
                    values.add(names.get(key));
                }
                size = values.size();
            }
            if (size < eras.length) {
                int baseStyle = getBaseStyle(style);
                for (int i = size; i < eras.length; i++) {
                    Era era = eras[i];
                    if (baseStyle == ALL_STYLES || baseStyle == SHORT
                            || baseStyle == NARROW_FORMAT) {
                        names.put(era.getAbbreviation(), i);
                    }
                    if (baseStyle == ALL_STYLES || baseStyle == LONG) {
                        names.put(era.getName(), i);
                    }
                }
            }
        }
    }
    return names;
}
 
Example #27
Source File: DateTimeTextProvider.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #28
Source File: Calendar.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
 * They are used to figure out the week count for a specific date for
 * a given locale. These must be set when a Calendar is constructed.
 * @param desiredLocale the given locale.
 */
private void setWeekCountData(Locale desiredLocale)
{
    /* try to get the Locale data from the cache */
    int[] data = cachedLocaleData.get(desiredLocale);
    if (data == null) {  /* cache miss */
        data = new int[2];
        data[0] = CalendarDataUtility.retrieveFirstDayOfWeek(desiredLocale);
        data[1] = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(desiredLocale);
        cachedLocaleData.putIfAbsent(desiredLocale, data);
    }
    firstDayOfWeek = data[0];
    minimalDaysInFirstWeek = data[1];
}
 
Example #29
Source File: DateTimeTextProvider.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
 
Example #30
Source File: Calendar.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
 * They are used to figure out the week count for a specific date for
 * a given locale. These must be set when a Calendar is constructed.
 * @param desiredLocale the given locale.
 */
private void setWeekCountData(Locale desiredLocale)
{
    /* try to get the Locale data from the cache */
    int[] data = cachedLocaleData.get(desiredLocale);
    if (data == null) {  /* cache miss */
        data = new int[2];
        data[0] = CalendarDataUtility.retrieveFirstDayOfWeek(desiredLocale);
        data[1] = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(desiredLocale);
        cachedLocaleData.putIfAbsent(desiredLocale, data);
    }
    firstDayOfWeek = data[0];
    minimalDaysInFirstWeek = data[1];
}