/* * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved * (C) Copyright IBM Corp. 1996 - All Rights Reserved * * The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */ package java.text; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.ref.SoftReference; import java.text.spi.DateFormatSymbolsProvider; import java.util.Arrays; import java.util.Locale; import java.util.Objects; import java.util.ResourceBundle; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import sun.util.locale.provider.CalendarDataUtility; import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleServiceProviderPool; import sun.util.locale.provider.ResourceBundleBasedAdapter; import sun.util.locale.provider.TimeZoneNameUtility; /** * {@code DateFormatSymbols} is a public class for encapsulating * localizable date-time formatting data, such as the names of the * months, the names of the days of the week, and the time zone data. * {@code SimpleDateFormat} uses * {@code DateFormatSymbols} to encapsulate this information. * *
* Typically you shouldn't use {@code DateFormatSymbols} directly. * Rather, you are encouraged to create a date-time formatter with the * {@code DateFormat} class's factory methods: {@code getTimeInstance}, * {@code getDateInstance}, or {@code getDateTimeInstance}. * These methods automatically create a {@code DateFormatSymbols} for * the formatter so that you don't have to. After the * formatter is created, you may modify its format pattern using the * {@code setPattern} method. For more information about * creating formatters using {@code DateFormat}'s factory methods, * see {@link DateFormat}. * *
* If you decide to create a date-time formatter with a specific * format pattern for a specific locale, you can do so with: *
** ** new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)). **
If the locale contains "rg" (region override) * Unicode extension, * the symbols are overridden for the designated region. * *
* {@code DateFormatSymbols} objects are cloneable. When you obtain * a {@code DateFormatSymbols} object, feel free to modify the * date-time formatting data. For instance, you can replace the localized * date-time format pattern characters with the ones that you feel easy * to remember. Or you can change the representative cities * to your favorite ones. * *
* New {@code DateFormatSymbols} subclasses may be added to support * {@code SimpleDateFormat} for date-time formatting for additional locales. * * @see DateFormat * @see SimpleDateFormat * @see java.util.SimpleTimeZone * @author Chen-Lieh Huang * @since 1.1 */ public class DateFormatSymbols implements Serializable, Cloneable { /** * Construct a DateFormatSymbols object by loading format data from * resources for the default {@link java.util.Locale.Category#FORMAT FORMAT} * locale. This constructor can only * construct instances for the locales supported by the Java * runtime environment, not for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. For full locale coverage, use the * {@link #getInstance(Locale) getInstance} method. *
This is equivalent to calling * {@link #DateFormatSymbols(Locale) * DateFormatSymbols(Locale.getDefault(Locale.Category.FORMAT))}. * @see #getInstance() * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT * @throws java.util.MissingResourceException * if the resources for the default locale cannot be * found or cannot be loaded. */ public DateFormatSymbols() { initializeData(Locale.getDefault(Locale.Category.FORMAT)); } /** * Construct a DateFormatSymbols object by loading format data from * resources for the given locale. This constructor can only * construct instances for the locales supported by the Java * runtime environment, not for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. For full locale coverage, use the * {@link #getInstance(Locale) getInstance} method. * * @param locale the desired locale * @see #getInstance(Locale) * @throws java.util.MissingResourceException * if the resources for the specified locale cannot be * found or cannot be loaded. */ public DateFormatSymbols(Locale locale) { initializeData(locale); } /** * Constructs an uninitialized DateFormatSymbols. */ private DateFormatSymbols(boolean flag) { } /** * Era strings. For example: "AD" and "BC". An array of 2 strings, * indexed by {@code Calendar.BC} and {@code Calendar.AD}. * @serial */ String eras[] = null; /** * Month strings. For example: "January", "February", etc. An array * of 13 strings (some calendars have 13 months), indexed by * {@code Calendar.JANUARY}, {@code Calendar.FEBRUARY}, etc. * @serial */ String months[] = null; /** * Short month strings. For example: "Jan", "Feb", etc. An array of * 13 strings (some calendars have 13 months), indexed by * {@code Calendar.JANUARY}, {@code Calendar.FEBRUARY}, etc. * @serial */ String shortMonths[] = null; /** * Weekday strings. For example: "Sunday", "Monday", etc. An array * of 8 strings, indexed by {@code Calendar.SUNDAY}, * {@code Calendar.MONDAY}, etc. * The element {@code weekdays[0]} is ignored. * @serial */ String weekdays[] = null; /** * Short weekday strings. For example: "Sun", "Mon", etc. An array * of 8 strings, indexed by {@code Calendar.SUNDAY}, * {@code Calendar.MONDAY}, etc. * The element {@code shortWeekdays[0]} is ignored. * @serial */ String shortWeekdays[] = null; /** * AM and PM strings. For example: "AM" and "PM". An array of * 2 strings, indexed by {@code Calendar.AM} and * {@code Calendar.PM}. * @serial */ String ampms[] = null; /** * Localized names of time zones in this locale. This is a * two-dimensional array of strings of size n by m, * where m is at least 5. Each of the n rows is an * entry containing the localized names for a single {@code TimeZone}. * Each such row contains (with {@code i} ranging from * 0..n-1): *
This is equivalent to calling {@link #getInstance(Locale) * getInstance(Locale.getDefault(Locale.Category.FORMAT))}. * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT * @return a {@code DateFormatSymbols} instance. * @since 1.6 */ public static final DateFormatSymbols getInstance() { return getInstance(Locale.getDefault(Locale.Category.FORMAT)); } /** * Gets the {@code DateFormatSymbols} instance for the specified * locale. This method provides access to {@code DateFormatSymbols} * instances for locales supported by the Java runtime itself as well * as for those supported by installed * {@link java.text.spi.DateFormatSymbolsProvider DateFormatSymbolsProvider} * implementations. * @param locale the given locale. * @return a {@code DateFormatSymbols} instance. * @throws NullPointerException if {@code locale} is null * @since 1.6 */ public static final DateFormatSymbols getInstance(Locale locale) { DateFormatSymbols dfs = getProviderInstance(locale); if (dfs != null) { return dfs; } throw new RuntimeException("DateFormatSymbols instance creation failed."); } /** * Returns a DateFormatSymbols provided by a provider or found in * the cache. Note that this method returns a cached instance, * not its clone. Therefore, the instance should never be given to * an application. */ static final DateFormatSymbols getInstanceRef(Locale locale) { DateFormatSymbols dfs = getProviderInstance(locale); if (dfs != null) { return dfs; } throw new RuntimeException("DateFormatSymbols instance creation failed."); } private static DateFormatSymbols getProviderInstance(Locale locale) { LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatSymbolsProvider.class, locale); DateFormatSymbolsProvider provider = adapter.getDateFormatSymbolsProvider(); DateFormatSymbols dfsyms = provider.getInstance(locale); if (dfsyms == null) { provider = LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider(); dfsyms = provider.getInstance(locale); } return dfsyms; } /** * Gets era strings. For example: "AD" and "BC". * @return the era strings. */ public String[] getEras() { return Arrays.copyOf(eras, eras.length); } /** * Sets era strings. For example: "AD" and "BC". * @param newEras the new era strings. */ public void setEras(String[] newEras) { eras = Arrays.copyOf(newEras, newEras.length); cachedHashCode = 0; } /** * Gets month strings. For example: "January", "February", etc. * An array with either 12 or 13 elements will be returned depending * on whether or not {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER} * is supported. Use * {@link java.util.Calendar#JANUARY Calendar.JANUARY}, * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, * etc. to index the result array. * *
If the language requires different forms for formatting and * stand-alone usages, this method returns month names in the * formatting form. For example, the preferred month name for * January in the Czech language is ledna in the * formatting form, while it is leden in the stand-alone * form. This method returns {@code "ledna"} in this case. Refer * to the * Calendar Elements in the Unicode Locale Data Markup Language * (LDML) specification for more details. * * @implSpec This method returns 13 elements since * {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER} is supported. * @return the month strings. */ public String[] getMonths() { return Arrays.copyOf(months, months.length); } /** * Sets month strings. For example: "January", "February", etc. * @param newMonths the new month strings. The array should * be indexed by {@link java.util.Calendar#JANUARY Calendar.JANUARY}, * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, etc. */ public void setMonths(String[] newMonths) { months = Arrays.copyOf(newMonths, newMonths.length); cachedHashCode = 0; } /** * Gets short month strings. For example: "Jan", "Feb", etc. * An array with either 12 or 13 elements will be returned depending * on whether or not {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER} * is supported. Use * {@link java.util.Calendar#JANUARY Calendar.JANUARY}, * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, * etc. to index the result array. * *
If the language requires different forms for formatting and * stand-alone usages, this method returns short month names in * the formatting form. For example, the preferred abbreviation * for January in the Catalan language is de gen. in the * formatting form, while it is gen. in the stand-alone * form. This method returns {@code "de gen."} in this case. Refer * to the * Calendar Elements in the Unicode Locale Data Markup Language * (LDML) specification for more details. * * @implSpec This method returns 13 elements since * {@link java.util.Calendar#UNDECIMBER Calendar.UNDECIMBER} is supported. * @return the short month strings. */ public String[] getShortMonths() { return Arrays.copyOf(shortMonths, shortMonths.length); } /** * Sets short month strings. For example: "Jan", "Feb", etc. * @param newShortMonths the new short month strings. The array should * be indexed by {@link java.util.Calendar#JANUARY Calendar.JANUARY}, * {@link java.util.Calendar#FEBRUARY Calendar.FEBRUARY}, etc. */ public void setShortMonths(String[] newShortMonths) { shortMonths = Arrays.copyOf(newShortMonths, newShortMonths.length); cachedHashCode = 0; } /** * Gets weekday strings. For example: "Sunday", "Monday", etc. * @return the weekday strings. Use * {@link java.util.Calendar#SUNDAY Calendar.SUNDAY}, * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. to index * the result array. */ public String[] getWeekdays() { return Arrays.copyOf(weekdays, weekdays.length); } /** * Sets weekday strings. For example: "Sunday", "Monday", etc. * @param newWeekdays the new weekday strings. The array should * be indexed by {@link java.util.Calendar#SUNDAY Calendar.SUNDAY}, * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. */ public void setWeekdays(String[] newWeekdays) { weekdays = Arrays.copyOf(newWeekdays, newWeekdays.length); cachedHashCode = 0; } /** * Gets short weekday strings. For example: "Sun", "Mon", etc. * @return the short weekday strings. Use * {@link java.util.Calendar#SUNDAY Calendar.SUNDAY}, * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. to index * the result array. */ public String[] getShortWeekdays() { return Arrays.copyOf(shortWeekdays, shortWeekdays.length); } /** * Sets short weekday strings. For example: "Sun", "Mon", etc. * @param newShortWeekdays the new short weekday strings. The array should * be indexed by {@link java.util.Calendar#SUNDAY Calendar.SUNDAY}, * {@link java.util.Calendar#MONDAY Calendar.MONDAY}, etc. */ public void setShortWeekdays(String[] newShortWeekdays) { shortWeekdays = Arrays.copyOf(newShortWeekdays, newShortWeekdays.length); cachedHashCode = 0; } /** * Gets ampm strings. For example: "AM" and "PM". * @return the ampm strings. */ public String[] getAmPmStrings() { return Arrays.copyOf(ampms, ampms.length); } /** * Sets ampm strings. For example: "AM" and "PM". * @param newAmpms the new ampm strings. */ public void setAmPmStrings(String[] newAmpms) { ampms = Arrays.copyOf(newAmpms, newAmpms.length); cachedHashCode = 0; } /** * Gets time zone strings. Use of this method is discouraged; use * {@link java.util.TimeZone#getDisplayName() TimeZone.getDisplayName()} * instead. *
* The value returned is a * two-dimensional array of strings of size n by m, * where m is at least 5. Each of the n rows is an * entry containing the localized names for a single {@code TimeZone}. * Each such row contains (with {@code i} ranging from * 0..n-1): *
* If {@link #setZoneStrings(String[][]) setZoneStrings} has been called * on this {@code DateFormatSymbols} instance, then the strings * provided by that call are returned. Otherwise, the returned array * contains names provided by the Java runtime and by installed * {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} * implementations. * * @return the time zone strings. * @see #setZoneStrings(String[][]) */ public String[][] getZoneStrings() { return getZoneStringsImpl(true); } /** * Sets time zone strings. The argument must be a * two-dimensional array of strings of size n by m, * where m is at least 5. Each of the n rows is an * entry containing the localized names for a single {@code TimeZone}. * Each such row contains (with {@code i} ranging from * 0..n-1): *