Import GNU Classpath (20121202).
[official-gcc.git] / libjava / classpath / java / text / DateFormatSymbols.java
blob9d0ace65a3ec95faaa8529698489765f7c6796f1
1 /* DateFormatSymbols.java -- Format over a range of numbers
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.text;
41 import gnu.java.locale.LocaleHelper;
43 import java.io.IOException;
45 import java.text.spi.DateFormatSymbolsProvider;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Locale;
52 import java.util.Map;
53 import java.util.MissingResourceException;
54 import java.util.Properties;
55 import java.util.ResourceBundle;
56 import java.util.ServiceLoader;
57 import java.util.TimeZone;
59 import java.util.concurrent.ConcurrentMap;
60 import java.util.concurrent.ConcurrentHashMap;
62 import java.util.regex.Pattern;
64 import java.util.spi.TimeZoneNameProvider;
66 /**
67 * This class acts as container for locale specific date/time formatting
68 * information such as the days of the week and the months of the year.
70 * @author Per Bothner (bothner@cygnus.com)
71 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
72 * @date October 24, 1998.
74 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
75 * Status: Believed complete and correct.
77 public class DateFormatSymbols implements java.io.Serializable, Cloneable
79 /**
80 * The set of properties for obtaining the metazone data.
82 private static transient final Properties properties;
84 /**
85 * Reads in the properties.
87 static
89 properties = new Properties();
90 try
92 properties.load(DateFormatSymbols.class.getResourceAsStream("metazones.properties"));
94 catch (IOException exception)
96 System.out.println("Failed to load weeks resource: " + exception);
100 private static final Pattern ZONE_SEP = Pattern.compile("\u00a9");
102 private static final Pattern FIELD_SEP = Pattern.compile("\u00ae");
105 * Class for storing DateFormatSymbols data parsed from the property files.
107 private static class DFSData
109 private String[] ampms;
110 private String[] eras;
111 private String localPatternChars;
112 private String[] months;
113 private String[] shortMonths;
114 private String[] weekdays;
115 private String[] shortWeekdays;
116 private String[] dateFormats;
117 private String[] timeFormats;
118 private String[][] runtimeZoneStrings;
121 * Construct a new instance with the parsed data.
123 * @param ampms strings for "am" and "pm".
124 * @param eras strings for calendar eras.
125 * @param localPatternChars localised pattern characters.
126 * @param months strings for the months of the year.
127 * @param shortMonths short strings for the months of the year.
128 * @param weekdays strings for the days of the week.
129 * @param shortWeekdays short strings for the days of the week.
130 * @param dateFormats localised date formats.
131 * @param timeFormats localised time formats.
132 * @param runtimeZoneStrings localised time zone names.
134 public DFSData(String[] ampms, String[] eras, String localPatternChars,
135 String[] months, String[] shortMonths, String[] weekdays,
136 String[] shortWeekdays, String[] dateFormats,
137 String[] timeFormats, String[][] runtimeZoneStrings)
139 this.ampms = ampms;
140 this.eras = eras;
141 this.localPatternChars = localPatternChars;
142 this.months = months;
143 this.shortMonths = shortMonths;
144 this.weekdays = weekdays;
145 this.shortWeekdays = shortWeekdays;
146 this.dateFormats = dateFormats;
147 this.timeFormats = timeFormats;
148 this.runtimeZoneStrings = runtimeZoneStrings;
152 * Accessor for the AM/PM data.
154 * @return the AM/PM strings.
156 public String[] getAMPMs()
158 return ampms.clone();
162 * Accessor for the era data.
164 * @return the era strings.
166 public String[] getEras()
168 return eras.clone();
172 * Accessor for the local pattern characters.
174 * @return the local pattern characters.
176 public String getLocalPatternChars()
178 return localPatternChars;
182 * Accessor for the months of the year (long form).
184 * @return the months of the year (long form).
186 public String[] getMonths()
188 return months.clone();
192 * Accessor for the months of the year (short form).
194 * @return the months of the year (short form).
196 public String[] getShortMonths()
198 return shortMonths.clone();
202 * Accessor for the days of the week (long form).
204 * @return the days of the week (long form).
206 public String[] getWeekdays()
208 return weekdays.clone();
212 * Accessor for the days of the week (short form).
214 * @return the days of the week (short form).
216 public String[] getShortWeekdays()
218 return shortWeekdays.clone();
222 * Accessor for the date formats.
224 * @return the date formats.
226 public String[] getDateFormats()
228 return dateFormats.clone();
232 * Accessor for the time formats.
234 * @return the time formats.
236 public String[] getTimeFormats()
238 return timeFormats.clone();
242 * Accessor for the zone strings.
244 * @return the zone strings.
246 public String[][] getZoneStrings()
248 // Perform a deep clone so subarrays aren't modifiable
249 String[][] clone = runtimeZoneStrings.clone();
250 for (int a = 0; a < clone.length; ++a)
251 clone[a] = runtimeZoneStrings[a].clone();
252 return clone;
257 private static final ConcurrentMap<Locale, DFSData> dataCache = new ConcurrentHashMap<Locale, DFSData>();
259 String[] ampms;
260 String[] eras;
261 private String localPatternChars;
262 String[] months;
263 String[] shortMonths;
264 String[] shortWeekdays;
265 String[] weekdays;
268 * The timezone strings supplied by the runtime.
270 private String[][] runtimeZoneStrings;
273 * Custom timezone strings supplied by {@link #setZoneStrings()}.
275 private String[][] zoneStrings;
277 private static final long serialVersionUID = -5987973545549424702L;
279 // The order of these prefixes must be the same as in DateFormat
280 private static final String[] formatPrefixes =
282 "full", "long", "medium", "short"
285 // These are each arrays with a value for SHORT, MEDIUM, LONG, FULL,
286 // and DEFAULT (constants defined in java.text.DateFormat). While
287 // not part of the official spec, we need a way to get at locale-specific
288 // default formatting patterns. They are declared package scope so
289 // as to be easily accessible where needed (DateFormat, SimpleDateFormat).
290 transient String[] dateFormats;
291 transient String[] timeFormats;
294 * Compiles a string array for a property using data from each of the locales in the
295 * hierarchy as necessary.
297 * @param bundles the locale hierarchy, starting with the most specific.
298 * @param name the name of the property.
299 * @param size the size the array should be when complete.
300 * @return a completed string array.
302 private static String[] getStringArray(List<ResourceBundle> bundles, String name, int size)
304 return getStringArray(bundles, name, size, null);
308 * Compiles a string array for a property using data from each of the locales in the
309 * hierarchy as necessary. If non-null, the fallback array is also used for "sideways"
310 * inheritance (e.g. if there is no short name for a month, the long name is used rather
311 * than the empty string).
313 * @param bundles the locale hierarchy, starting with the most specific.
314 * @param name the name of the property.
315 * @param size the size the array should be when complete.
316 * @param fallback an array of long name fallback strings for data with both long and short names.
317 * @return a completed string array.
319 private static String[] getStringArray(List<ResourceBundle> bundles, String name, int size,
320 String[] fallback)
322 String[] data = new String[size];
323 Arrays.fill(data, "");
324 // Populate array with data from each locale back to the root, starting with the most specific
325 for (int a = 0; a < bundles.size(); ++a)
327 String localeData = bundles.get(a).getString(name);
328 String[] array = FIELD_SEP.split(localeData, size);
329 for (int b = 0; b < data.length; ++b)
331 if (array.length > b && array[b] != null && data[b].isEmpty() && !array[b].isEmpty())
332 data[b] = array[b];
335 // Replace any remaining empty strings with data from the fallback array, if non-null
336 if (fallback != null && fallback.length == size)
338 for (int a = 0; a < data.length; ++a)
340 if (data[a].isEmpty() && fallback[a] != null && !fallback[a].isEmpty())
341 data[a] = fallback[a];
344 return data;
347 private static String[][] getZoneStrings(List<ResourceBundle> bundles, Locale locale)
349 List<String[]> allZones = new ArrayList<String[]>();
352 Map<String,String[]> systemZones = new HashMap<String,String[]>();
353 for (ResourceBundle bundle : bundles)
355 String country = locale.getCountry();
356 String data = bundle.getString("zoneStrings");
357 String[] zones = ZONE_SEP.split(data);
358 for (int a = 0; a < zones.length; ++a)
360 String[] strings = FIELD_SEP.split(zones[a]);
361 String type = properties.getProperty(strings[0] + "." + country);
362 if (type == null)
363 type = properties.getProperty(strings[0] + ".DEFAULT");
364 if (type != null)
365 strings[0] = type;
366 if (strings.length < 5)
368 String[] newStrings = new String[5];
369 System.arraycopy(strings, 0, newStrings, 0, strings.length);
370 for (int b = strings.length; b < newStrings.length; ++b)
371 newStrings[b] = "";
372 strings = newStrings;
374 String[] existing = systemZones.get(strings[0]);
375 if (existing != null && existing.length > 1)
377 for (int b = 1; b < existing.length; ++b)
378 if (!existing[b].equals(""))
379 strings[b] = existing[b];
381 systemZones.put(strings[0], strings);
384 /* Final sanity check for missing values */
385 for (String[] zstrings : systemZones.values())
387 if (zstrings[1].equals("") && zstrings[2].equals(""))
389 for (Map.Entry<Object,Object> entry : properties.entrySet())
391 String val = (String) entry.getValue();
392 if (val.equals(zstrings[0]))
394 String key = (String) entry.getKey();
395 String metazone = key.substring(0, key.indexOf("."));
396 String type = properties.getProperty(metazone + "." + locale.getCountry());
397 if (type == null)
398 type = properties.getProperty(metazone + ".DEFAULT");
399 if (type != null)
401 String[] ostrings = systemZones.get(type);
402 zstrings[1] = ostrings[1];
403 zstrings[2] = ostrings[2];
409 allZones.addAll(systemZones.values());
411 catch (MissingResourceException e)
413 /* This means runtime support for the locale
414 * is not available, so we just include providers. */
416 for (TimeZoneNameProvider p :
417 ServiceLoader.load(TimeZoneNameProvider.class))
419 for (Locale loc : p.getAvailableLocales())
421 if (loc.equals(locale))
423 for (String id : TimeZone.getAvailableIDs())
425 String[] z = new String[5];
426 z[0] = id;
427 z[1] = p.getDisplayName(id, false,
428 TimeZone.LONG,
429 locale);
430 z[2] = p.getDisplayName(id, false,
431 TimeZone.SHORT,
432 locale);
433 z[3] = p.getDisplayName(id, true,
434 TimeZone.LONG,
435 locale);
436 z[4] = p.getDisplayName(id, true,
437 TimeZone.SHORT,
438 locale);
439 allZones.add(z);
441 break;
445 return allZones.toArray(new String[allZones.size()][]);
449 * Retrieve the date or time formats for a specific key e.g.
450 * asking for "DateFormat" will return an array containing the
451 * full, long, medium and short date formats localised for
452 * the locales in the specified bundle.
454 * @param bundles the stack of bundles to check, most-specific first.
455 * @param key the type of format to retrieve.
456 * @param an array of localised strings for each format prefix.
458 private static String[] formatsForKey(List<ResourceBundle> bundles, String key)
460 String[] values = new String[formatPrefixes.length];
462 for (int i = 0; i < formatPrefixes.length; i++)
463 values[i] = getString(bundles, formatPrefixes[i] + key);
465 return values;
469 * Simple wrapper around extracting a {@code String} from a
470 * {@code ResourceBundle}. Keep searching less-specific locales
471 * until a non-null non-empty value is found.
473 * @param bundles the stack of bundles to check, most-specific first.
474 * @param key the key of the value to retrieve.
475 * @return the first non-null non-empty String found or the last
476 * retrieved if one isn't found.
478 private static String getString(List<ResourceBundle> bundles, String key)
480 String val = null;
481 for (ResourceBundle bundle : bundles)
483 val = bundle.getString(key);
484 if (val != null && !val.isEmpty())
485 return val;
487 return val;
491 * Retrieves the locale data from the property files and constructs a
492 * {@code DFSData} instance for it.
494 * @param the locale for which data should be retrieved.
495 * @return the parsed data.
496 * @throws MissingResourceException if the resources for the specified
497 * locale could not be found or loaded.
499 private static DFSData retrieveData(Locale locale)
500 throws MissingResourceException
502 DFSData data = dataCache.get(locale);
503 if (data == null)
505 ClassLoader ldr = ClassLoader.getSystemClassLoader();
506 List<ResourceBundle> bundles = new ArrayList<ResourceBundle>();
507 ResourceBundle res
508 = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale, ldr);
509 bundles.add(res);
510 Locale resLocale = res.getLocale();
511 while (resLocale != Locale.ROOT)
513 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
514 LocaleHelper.getFallbackLocale(resLocale), ldr);
515 bundles.add(res);
516 resLocale = res.getLocale();
518 String[] lMonths = getStringArray(bundles, "months", 13);
519 String[] lWeekdays = getStringArray(bundles, "weekdays", 8);
520 data = new DFSData(getStringArray(bundles, "ampms", 2),
521 getStringArray(bundles, "eras", 2),
522 getString(bundles, "localPatternChars"),
523 lMonths, getStringArray(bundles, "shortMonths", 13, lMonths),
524 lWeekdays, getStringArray(bundles, "shortWeekdays", 8, lWeekdays),
525 formatsForKey(bundles, "DateFormat"),
526 formatsForKey(bundles, "TimeFormat"),
527 getZoneStrings(bundles, locale));
528 DFSData cachedData = dataCache.putIfAbsent(locale, data);
529 // Use the earlier version if another thread beat us to it.
530 if (cachedData != null)
531 data = cachedData;
533 return data;
537 * This method initializes a new instance of <code>DateFormatSymbols</code>
538 * by loading the date format information for the specified locale.
539 * This constructor only obtains instances using the runtime's resources;
540 * to also include {@link java.text.spi.DateFormatSymbolsProvider} instances,
541 * call {@link #getInstance(java.util.Locale)} instead.
543 * @param locale The locale for which date formatting symbols should
544 * be loaded.
545 * @throws MissingResourceException if the resources for the specified
546 * locale could not be found or loaded.
547 * @see #getInstance(java.util.Locale)
549 public DateFormatSymbols (Locale locale)
550 throws MissingResourceException
552 DFSData data = retrieveData(locale);
553 ampms = data.getAMPMs();
554 eras = data.getEras();
555 localPatternChars = data.getLocalPatternChars();
556 months = data.getMonths();
557 shortMonths = data.getShortMonths();
558 weekdays = data.getWeekdays();
559 shortWeekdays = data.getShortWeekdays();
560 dateFormats = data.getDateFormats();
561 timeFormats = data.getTimeFormats();
562 runtimeZoneStrings = data.getZoneStrings();
566 * This method loads the format symbol information for the default
567 * locale. This constructor only obtains instances using the runtime's resources;
568 * to also include {@link java.text.spi.DateFormatSymbolsProvider} instances,
569 * call {@link #getInstance()} instead.
571 * @throws MissingResourceException if the resources for the default
572 * locale could not be found or loaded.
573 * @see #getInstance()
575 public DateFormatSymbols()
576 throws MissingResourceException
578 this (Locale.getDefault());
582 * This method returns the list of strings used for displaying AM or PM.
583 * This is a two element <code>String</code> array indexed by
584 * <code>Calendar.AM</code> and <code>Calendar.PM</code>
586 * @return The list of AM/PM display strings.
588 public String[] getAmPmStrings()
590 return ampms;
594 * This method returns the list of strings used for displaying eras
595 * (e.g., "BC" and "AD"). This is a two element <code>String</code>
596 * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
598 * @return The list of era disply strings.
600 public String[] getEras()
602 return eras;
606 * This method returns the pattern character information for this
607 * object. This is an 18 character string that contains the characters
608 * that are used in creating the date formatting strings in
609 * <code>SimpleDateFormat</code>. The following are the character
610 * positions in the string and which format character they correspond
611 * to (the character in parentheses is the default value in the US English
612 * locale):
613 * <p>
614 * <ul>
615 * <li>0 - era (G)</li>
616 * <li>1 - year (y)</li>
617 * <li>2 - month (M)</li>
618 * <li>3 - day of month (d)</li>
619 * <li>4 - hour out of 12, from 1-12 (h)</li>
620 * <li>5 - hour out of 24, from 0-23 (H)</li>
621 * <li>6 - minute (m)</li>
622 * <li>7 - second (s)</li>
623 * <li>8 - millisecond (S)</li>
624 * <li>9 - date of week (E)</li>
625 * <li>10 - date of year (D)</li>
626 * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
627 * <li>12 - week in year (w)</li>
628 * <li>13 - week in month (W)</li>
629 * <li>14 - am/pm (a)</li>
630 * <li>15 - hour out of 24, from 1-24 (k)</li>
631 * <li>16 - hour out of 12, from 0-11 (K)</li>
632 * <li>17 - time zone (z)</li>
633 * </ul>
635 * @return The format patter characters
637 public String getLocalPatternChars()
639 return localPatternChars;
643 * This method returns the list of strings used for displaying month
644 * names (e.g., "January" and "February"). This is a thirteen element
645 * string array indexed by <code>Calendar.JANUARY</code> through
646 * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
647 * elements because some calendars have thriteen months.
649 * @return The list of month display strings.
651 public String[] getMonths ()
653 return months;
657 * This method returns the list of strings used for displaying abbreviated
658 * month names (e.g., "Jan" and "Feb"). This is a thirteen element
659 * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
660 * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
661 * elements because some calendars have thirteen months.
663 * @return The list of abbreviated month display strings.
665 public String[] getShortMonths ()
667 return shortMonths;
671 * This method returns the list of strings used for displaying abbreviated
672 * weekday names (e.g., "Sun" and "Mon"). This is an eight element
673 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
674 * through <code>Calendar.SATURDAY</code>. Note that the first element
675 * of this array is ignored.
677 * @return This list of abbreviated weekday display strings.
679 public String[] getShortWeekdays ()
681 return shortWeekdays;
685 * This method returns the list of strings used for displaying weekday
686 * names (e.g., "Sunday" and "Monday"). This is an eight element
687 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
688 * through <code>Calendar.SATURDAY</code>. Note that the first element
689 * of this array is ignored.
691 * @return This list of weekday display strings.
693 public String[] getWeekdays ()
695 return weekdays;
699 * This method returns this list of localized timezone display strings.
700 * This is a two dimensional <code>String</code> array where each row in
701 * the array contains five values:
702 * <P>
703 * <ul>
704 * <li>0 - The non-localized time zone id string.</li>
705 * <li>1 - The long name of the time zone (standard time).</li>
706 * <li>2 - The short name of the time zone (standard time).</li>
707 * <li>3 - The long name of the time zone (daylight savings time).</li>
708 * <li>4 - the short name of the time zone (daylight savings time).</li>
709 * </ul>
710 * <p>
711 * If {@link #setZoneStrings(String[][])} has been called, then the value
712 * passed to this will be returned. Otherwise the returned array contains
713 * zone names provided by the runtime environment and any
714 * {@link java.util.spi.TimeZoneProvider} instances.
715 * </p>
717 * @return The list of time zone display strings.
718 * @see #setZoneStrings(String[][])
720 public String[][] getZoneStrings()
722 if (zoneStrings != null)
723 return zoneStrings;
724 return runtimeZoneStrings;
728 * This method sets the list of strings used to display AM/PM values to
729 * the specified list.
730 * This is a two element <code>String</code> array indexed by
731 * <code>Calendar.AM</code> and <code>Calendar.PM</code>
733 * @param value The new list of AM/PM display strings.
735 public void setAmPmStrings (String[] value)
737 if(value==null)
738 throw new NullPointerException();
739 ampms = value;
743 * This method sets the list of strings used to display time eras to
744 * to the specified list.
745 * This is a two element <code>String</code>
746 * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
748 * @param labels The new list of era display strings.
750 public void setEras (String[] labels)
752 if(labels==null)
753 throw new NullPointerException();
754 eras = labels;
758 * This method sets the list of characters used to specific date/time
759 * formatting strings.
760 * This is an 18 character string that contains the characters
761 * that are used in creating the date formatting strings in
762 * <code>SimpleDateFormat</code>. The following are the character
763 * positions in the string and which format character they correspond
764 * to (the character in parentheses is the default value in the US English
765 * locale):
766 * <p>
767 * <ul>
768 * <li>0 - era (G)</li>
769 * <li>1 - year (y)</li>
770 * <li>2 - month (M)</li>
771 * <li>3 - day of month (d)</li>
772 * <li>4 - hour out of 12, from 1-12 (h)</li>
773 * <li>5 - hour out of 24, from 0-23 (H)</li>
774 * <li>6 - minute (m)</li>
775 * <li>7 - second (s)</li>
776 * <li>8 - millisecond (S)</li>
777 * <li>9 - date of week (E)</li>
778 * <li>10 - date of year (D)</li>
779 * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
780 * <li>12 - week in year (w)</li>
781 * <li>13 - week in month (W)</li>
782 * <li>14 - am/pm (a)</li>
783 * <li>15 - hour out of 24, from 1-24 (k)</li>
784 * <li>16 - hour out of 12, from 0-11 (K)</li>
785 * <li>17 - time zone (z)</li>
786 * </ul>
788 * @param chars The new format pattern characters
790 public void setLocalPatternChars (String chars)
792 if(chars==null)
793 throw new NullPointerException();
794 localPatternChars = chars;
798 * This method sets the list of strings used to display month names.
799 * This is a thirteen element
800 * string array indexed by <code>Calendar.JANUARY</code> through
801 * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
802 * elements because some calendars have thriteen months.
804 * @param labels The list of month display strings.
806 public void setMonths (String[] labels)
808 if(labels==null)
809 throw new NullPointerException();
810 months = labels;
814 * This method sets the list of strings used to display abbreviated month
815 * names.
816 * This is a thirteen element
817 * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
818 * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
819 * elements because some calendars have thirteen months.
821 * @param labels The new list of abbreviated month display strings.
823 public void setShortMonths (String[] labels)
825 if(labels==null)
826 throw new NullPointerException();
827 shortMonths = labels;
831 * This method sets the list of strings used to display abbreviated
832 * weekday names.
833 * This is an eight element
834 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
835 * through <code>Calendar.SATURDAY</code>. Note that the first element
836 * of this array is ignored.
838 * @param labels This list of abbreviated weekday display strings.
840 public void setShortWeekdays (String[] labels)
842 if(labels==null)
843 throw new NullPointerException();
844 shortWeekdays = labels;
848 * This method sets the list of strings used to display weekday names.
849 * This is an eight element
850 * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
851 * through <code>Calendar.SATURDAY</code>. Note that the first element
852 * of this array is ignored.
854 * @param labels This list of weekday display strings.
856 public void setWeekdays (String[] labels)
858 if(labels==null)
859 throw new NullPointerException();
860 weekdays = labels;
864 * This method sets the list of display strings for time zones.
865 * This is a two dimensional <code>String</code> array where each row in
866 * the array contains five values:
867 * <P>
868 * <ul>
869 * <li>0 - The non-localized time zone id string.</li>
870 * <li>1 - The long name of the time zone (standard time).</li>
871 * <li>2 - The short name of the time zone (standard time).</li>
872 * <li>3 - The long name of the time zone (daylight savings time).</li>
873 * <li>4 - the short name of the time zone (daylight savings time).</li>
874 * </ul>
876 * @params zones The list of time zone display strings.
878 public void setZoneStrings (String[][] zones)
880 if(zones==null)
881 throw new NullPointerException();
882 zoneStrings = zones;
885 /* Does a "deep" equality test - recurses into arrays. */
886 private static boolean equals (Object x, Object y)
888 if (x == y)
889 return true;
890 if (x == null || y == null)
891 return false;
892 if (! (x instanceof Object[]) || ! (y instanceof Object[]))
893 return x.equals(y);
894 Object[] xa = (Object[]) x;
895 Object[] ya = (Object[]) y;
896 if (xa.length != ya.length)
897 return false;
898 for (int i = xa.length; --i >= 0; )
900 if (! equals(xa[i], ya[i]))
901 return false;
903 return true;
906 private static int hashCode (Object x)
908 if (x == null)
909 return 0;
910 if (! (x instanceof Object[]))
911 return x.hashCode();
912 Object[] xa = (Object[]) x;
913 int hash = 0;
914 for (int i = 0; i < xa.length; i++)
915 hash = 37 * hashCode(xa[i]);
916 return hash;
920 * This method tests a specified object for equality against this object.
921 * This will be true if and only if the specified object:
922 * <p>
923 * <ul>
924 * <li> Is not <code>null</code>.</li>
925 * <li> Is an instance of <code>DateFormatSymbols</code>.</li>
926 * <li> Contains identical formatting symbols to this object.</li>
927 * </ul>
929 * @param obj The <code>Object</code> to test for equality against.
931 * @return <code>true</code> if the specified object is equal to this one,
932 * <code>false</code> otherwise.
934 public boolean equals (Object obj)
936 if (! (obj instanceof DateFormatSymbols))
937 return false;
938 DateFormatSymbols other = (DateFormatSymbols) obj;
939 return (equals(ampms, other.ampms)
940 && equals(eras, other.eras)
941 && equals(localPatternChars, other.localPatternChars)
942 && equals(months, other.months)
943 && equals(shortMonths, other.shortMonths)
944 && equals(shortWeekdays, other.shortWeekdays)
945 && equals(weekdays, other.weekdays)
946 && equals(zoneStrings, other.zoneStrings));
950 * Returns a new copy of this object.
952 * @return A copy of this object
954 public Object clone ()
958 return super.clone ();
960 catch (CloneNotSupportedException e)
962 return null;
967 * This method returns a hash value for this object.
969 * @return A hash value for this object.
971 public int hashCode ()
973 return (hashCode(ampms)
974 ^ hashCode(eras)
975 ^ hashCode(localPatternChars)
976 ^ hashCode(months)
977 ^ hashCode(shortMonths)
978 ^ hashCode(shortWeekdays)
979 ^ hashCode(weekdays)
980 ^ hashCode(zoneStrings));
984 * Returns a {@link DateFormatSymbols} instance for the
985 * default locale obtained from either the runtime itself
986 * or one of the installed
987 * {@link java.text.spi.DateFormatSymbolsProvider} instances.
988 * This is equivalent to calling
989 * <code>getInstance(Locale.getDefault())</code>.
991 * @return a {@link DateFormatSymbols} instance for the default
992 * locale.
993 * @since 1.6
995 public static final DateFormatSymbols getInstance()
997 return getInstance(Locale.getDefault());
1001 * Returns a {@link DateFormatSymbols} instance for the
1002 * specified locale obtained from either the runtime itself
1003 * or one of the installed
1004 * {@link java.text.spi.DateFormatSymbolsProvider} instances.
1006 * @param locale the locale for which an instance should be
1007 * returned.
1008 * @return a {@link DateFormatSymbols} instance for the specified
1009 * locale.
1010 * @throws NullPointerException if <code>locale</code> is
1011 * <code>null</code>.
1012 * @since 1.6
1014 public static final DateFormatSymbols getInstance(Locale locale)
1018 DateFormatSymbols syms = new DateFormatSymbols(locale);
1019 return syms;
1021 catch (MissingResourceException e)
1023 /* This means runtime support for the locale
1024 * is not available, so we check providers. */
1026 for (DateFormatSymbolsProvider p :
1027 ServiceLoader.load(DateFormatSymbolsProvider.class))
1029 for (Locale loc : p.getAvailableLocales())
1031 if (loc.equals(locale))
1033 DateFormatSymbols syms = p.getInstance(locale);
1034 if (syms != null)
1035 return syms;
1036 break;
1040 return getInstance(LocaleHelper.getFallbackLocale(locale));