DateFormatSymbols.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /* DateFormatSymbols.java -- Format over a range of numbers
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. import gnu.java.locale.LocaleHelper;
  33. import java.io.IOException;
  34. import java.text.spi.DateFormatSymbolsProvider;
  35. import java.util.ArrayList;
  36. import java.util.Arrays;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Locale;
  40. import java.util.Map;
  41. import java.util.MissingResourceException;
  42. import java.util.Properties;
  43. import java.util.ResourceBundle;
  44. import java.util.ServiceLoader;
  45. import java.util.TimeZone;
  46. import java.util.concurrent.ConcurrentMap;
  47. import java.util.concurrent.ConcurrentHashMap;
  48. import java.util.regex.Pattern;
  49. import java.util.spi.TimeZoneNameProvider;
  50. /**
  51. * This class acts as container for locale specific date/time formatting
  52. * information such as the days of the week and the months of the year.
  53. *
  54. * @author Per Bothner (bothner@cygnus.com)
  55. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  56. * @date October 24, 1998.
  57. */
  58. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3.
  59. * Status: Believed complete and correct.
  60. */
  61. public class DateFormatSymbols implements java.io.Serializable, Cloneable
  62. {
  63. /**
  64. * The set of properties for obtaining the metazone data.
  65. */
  66. private static transient final Properties properties;
  67. /**
  68. * Reads in the properties.
  69. */
  70. static
  71. {
  72. properties = new Properties();
  73. try
  74. {
  75. properties.load(DateFormatSymbols.class.getResourceAsStream("metazones.properties"));
  76. }
  77. catch (IOException exception)
  78. {
  79. System.out.println("Failed to load weeks resource: " + exception);
  80. }
  81. }
  82. private static final Pattern ZONE_SEP = Pattern.compile("\u00a9");
  83. private static final Pattern FIELD_SEP = Pattern.compile("\u00ae");
  84. /**
  85. * Class for storing DateFormatSymbols data parsed from the property files.
  86. */
  87. private static class DFSData
  88. {
  89. private String[] ampms;
  90. private String[] eras;
  91. private String localPatternChars;
  92. private String[] months;
  93. private String[] shortMonths;
  94. private String[] weekdays;
  95. private String[] shortWeekdays;
  96. private String[] dateFormats;
  97. private String[] timeFormats;
  98. private String[][] runtimeZoneStrings;
  99. /**
  100. * Construct a new instance with the parsed data.
  101. *
  102. * @param ampms strings for "am" and "pm".
  103. * @param eras strings for calendar eras.
  104. * @param localPatternChars localised pattern characters.
  105. * @param months strings for the months of the year.
  106. * @param shortMonths short strings for the months of the year.
  107. * @param weekdays strings for the days of the week.
  108. * @param shortWeekdays short strings for the days of the week.
  109. * @param dateFormats localised date formats.
  110. * @param timeFormats localised time formats.
  111. * @param runtimeZoneStrings localised time zone names.
  112. */
  113. public DFSData(String[] ampms, String[] eras, String localPatternChars,
  114. String[] months, String[] shortMonths, String[] weekdays,
  115. String[] shortWeekdays, String[] dateFormats,
  116. String[] timeFormats, String[][] runtimeZoneStrings)
  117. {
  118. this.ampms = ampms;
  119. this.eras = eras;
  120. this.localPatternChars = localPatternChars;
  121. this.months = months;
  122. this.shortMonths = shortMonths;
  123. this.weekdays = weekdays;
  124. this.shortWeekdays = shortWeekdays;
  125. this.dateFormats = dateFormats;
  126. this.timeFormats = timeFormats;
  127. this.runtimeZoneStrings = runtimeZoneStrings;
  128. }
  129. /**
  130. * Accessor for the AM/PM data.
  131. *
  132. * @return the AM/PM strings.
  133. */
  134. public String[] getAMPMs()
  135. {
  136. return ampms.clone();
  137. }
  138. /**
  139. * Accessor for the era data.
  140. *
  141. * @return the era strings.
  142. */
  143. public String[] getEras()
  144. {
  145. return eras.clone();
  146. }
  147. /**
  148. * Accessor for the local pattern characters.
  149. *
  150. * @return the local pattern characters.
  151. */
  152. public String getLocalPatternChars()
  153. {
  154. return localPatternChars;
  155. }
  156. /**
  157. * Accessor for the months of the year (long form).
  158. *
  159. * @return the months of the year (long form).
  160. */
  161. public String[] getMonths()
  162. {
  163. return months.clone();
  164. }
  165. /**
  166. * Accessor for the months of the year (short form).
  167. *
  168. * @return the months of the year (short form).
  169. */
  170. public String[] getShortMonths()
  171. {
  172. return shortMonths.clone();
  173. }
  174. /**
  175. * Accessor for the days of the week (long form).
  176. *
  177. * @return the days of the week (long form).
  178. */
  179. public String[] getWeekdays()
  180. {
  181. return weekdays.clone();
  182. }
  183. /**
  184. * Accessor for the days of the week (short form).
  185. *
  186. * @return the days of the week (short form).
  187. */
  188. public String[] getShortWeekdays()
  189. {
  190. return shortWeekdays.clone();
  191. }
  192. /**
  193. * Accessor for the date formats.
  194. *
  195. * @return the date formats.
  196. */
  197. public String[] getDateFormats()
  198. {
  199. return dateFormats.clone();
  200. }
  201. /**
  202. * Accessor for the time formats.
  203. *
  204. * @return the time formats.
  205. */
  206. public String[] getTimeFormats()
  207. {
  208. return timeFormats.clone();
  209. }
  210. /**
  211. * Accessor for the zone strings.
  212. *
  213. * @return the zone strings.
  214. */
  215. public String[][] getZoneStrings()
  216. {
  217. // Perform a deep clone so subarrays aren't modifiable
  218. String[][] clone = runtimeZoneStrings.clone();
  219. for (int a = 0; a < clone.length; ++a)
  220. clone[a] = runtimeZoneStrings[a].clone();
  221. return clone;
  222. }
  223. }
  224. private static final ConcurrentMap<Locale, DFSData> dataCache = new ConcurrentHashMap<Locale, DFSData>();
  225. String[] ampms;
  226. String[] eras;
  227. private String localPatternChars;
  228. String[] months;
  229. String[] shortMonths;
  230. String[] shortWeekdays;
  231. String[] weekdays;
  232. /**
  233. * The timezone strings supplied by the runtime.
  234. */
  235. private String[][] runtimeZoneStrings;
  236. /**
  237. * Custom timezone strings supplied by {@link #setZoneStrings()}.
  238. */
  239. private String[][] zoneStrings;
  240. private static final long serialVersionUID = -5987973545549424702L;
  241. // The order of these prefixes must be the same as in DateFormat
  242. private static final String[] formatPrefixes =
  243. {
  244. "full", "long", "medium", "short"
  245. };
  246. // These are each arrays with a value for SHORT, MEDIUM, LONG, FULL,
  247. // and DEFAULT (constants defined in java.text.DateFormat). While
  248. // not part of the official spec, we need a way to get at locale-specific
  249. // default formatting patterns. They are declared package scope so
  250. // as to be easily accessible where needed (DateFormat, SimpleDateFormat).
  251. transient String[] dateFormats;
  252. transient String[] timeFormats;
  253. /**
  254. * Compiles a string array for a property using data from each of the locales in the
  255. * hierarchy as necessary.
  256. *
  257. * @param bundles the locale hierarchy, starting with the most specific.
  258. * @param name the name of the property.
  259. * @param size the size the array should be when complete.
  260. * @return a completed string array.
  261. */
  262. private static String[] getStringArray(List<ResourceBundle> bundles, String name, int size)
  263. {
  264. return getStringArray(bundles, name, size, null);
  265. }
  266. /**
  267. * Compiles a string array for a property using data from each of the locales in the
  268. * hierarchy as necessary. If non-null, the fallback array is also used for "sideways"
  269. * inheritance (e.g. if there is no short name for a month, the long name is used rather
  270. * than the empty string).
  271. *
  272. * @param bundles the locale hierarchy, starting with the most specific.
  273. * @param name the name of the property.
  274. * @param size the size the array should be when complete.
  275. * @param fallback an array of long name fallback strings for data with both long and short names.
  276. * @return a completed string array.
  277. */
  278. private static String[] getStringArray(List<ResourceBundle> bundles, String name, int size,
  279. String[] fallback)
  280. {
  281. String[] data = new String[size];
  282. Arrays.fill(data, "");
  283. // Populate array with data from each locale back to the root, starting with the most specific
  284. for (int a = 0; a < bundles.size(); ++a)
  285. {
  286. String localeData = bundles.get(a).getString(name);
  287. String[] array = FIELD_SEP.split(localeData, size);
  288. for (int b = 0; b < data.length; ++b)
  289. {
  290. if (array.length > b && array[b] != null && data[b].isEmpty() && !array[b].isEmpty())
  291. data[b] = array[b];
  292. }
  293. }
  294. // Replace any remaining empty strings with data from the fallback array, if non-null
  295. if (fallback != null && fallback.length == size)
  296. {
  297. for (int a = 0; a < data.length; ++a)
  298. {
  299. if (data[a].isEmpty() && fallback[a] != null && !fallback[a].isEmpty())
  300. data[a] = fallback[a];
  301. }
  302. }
  303. return data;
  304. }
  305. private static String[][] getZoneStrings(List<ResourceBundle> bundles, Locale locale)
  306. {
  307. List<String[]> allZones = new ArrayList<String[]>();
  308. try
  309. {
  310. Map<String,String[]> systemZones = new HashMap<String,String[]>();
  311. for (ResourceBundle bundle : bundles)
  312. {
  313. String country = locale.getCountry();
  314. String data = bundle.getString("zoneStrings");
  315. String[] zones = ZONE_SEP.split(data);
  316. for (int a = 0; a < zones.length; ++a)
  317. {
  318. String[] strings = FIELD_SEP.split(zones[a]);
  319. String type = properties.getProperty(strings[0] + "." + country);
  320. if (type == null)
  321. type = properties.getProperty(strings[0] + ".DEFAULT");
  322. if (type != null)
  323. strings[0] = type;
  324. if (strings.length < 5)
  325. {
  326. String[] newStrings = new String[5];
  327. System.arraycopy(strings, 0, newStrings, 0, strings.length);
  328. for (int b = strings.length; b < newStrings.length; ++b)
  329. newStrings[b] = "";
  330. strings = newStrings;
  331. }
  332. String[] existing = systemZones.get(strings[0]);
  333. if (existing != null && existing.length > 1)
  334. {
  335. for (int b = 1; b < existing.length; ++b)
  336. if (!existing[b].equals(""))
  337. strings[b] = existing[b];
  338. }
  339. systemZones.put(strings[0], strings);
  340. }
  341. }
  342. /* Final sanity check for missing values */
  343. for (String[] zstrings : systemZones.values())
  344. {
  345. if (zstrings[1].equals("") && zstrings[2].equals(""))
  346. {
  347. for (Map.Entry<Object,Object> entry : properties.entrySet())
  348. {
  349. String val = (String) entry.getValue();
  350. if (val.equals(zstrings[0]))
  351. {
  352. String key = (String) entry.getKey();
  353. String metazone = key.substring(0, key.indexOf("."));
  354. String type = properties.getProperty(metazone + "." + locale.getCountry());
  355. if (type == null)
  356. type = properties.getProperty(metazone + ".DEFAULT");
  357. if (type != null)
  358. {
  359. String[] ostrings = systemZones.get(type);
  360. zstrings[1] = ostrings[1];
  361. zstrings[2] = ostrings[2];
  362. }
  363. }
  364. }
  365. }
  366. }
  367. allZones.addAll(systemZones.values());
  368. }
  369. catch (MissingResourceException e)
  370. {
  371. /* This means runtime support for the locale
  372. * is not available, so we just include providers. */
  373. }
  374. for (TimeZoneNameProvider p :
  375. ServiceLoader.load(TimeZoneNameProvider.class))
  376. {
  377. for (Locale loc : p.getAvailableLocales())
  378. {
  379. if (loc.equals(locale))
  380. {
  381. for (String id : TimeZone.getAvailableIDs())
  382. {
  383. String[] z = new String[5];
  384. z[0] = id;
  385. z[1] = p.getDisplayName(id, false,
  386. TimeZone.LONG,
  387. locale);
  388. z[2] = p.getDisplayName(id, false,
  389. TimeZone.SHORT,
  390. locale);
  391. z[3] = p.getDisplayName(id, true,
  392. TimeZone.LONG,
  393. locale);
  394. z[4] = p.getDisplayName(id, true,
  395. TimeZone.SHORT,
  396. locale);
  397. allZones.add(z);
  398. }
  399. break;
  400. }
  401. }
  402. }
  403. return allZones.toArray(new String[allZones.size()][]);
  404. }
  405. /**
  406. * Retrieve the date or time formats for a specific key e.g.
  407. * asking for "DateFormat" will return an array containing the
  408. * full, long, medium and short date formats localised for
  409. * the locales in the specified bundle.
  410. *
  411. * @param bundles the stack of bundles to check, most-specific first.
  412. * @param key the type of format to retrieve.
  413. * @param an array of localised strings for each format prefix.
  414. */
  415. private static String[] formatsForKey(List<ResourceBundle> bundles, String key)
  416. {
  417. String[] values = new String[formatPrefixes.length];
  418. for (int i = 0; i < formatPrefixes.length; i++)
  419. values[i] = getString(bundles, formatPrefixes[i] + key);
  420. return values;
  421. }
  422. /**
  423. * Simple wrapper around extracting a {@code String} from a
  424. * {@code ResourceBundle}. Keep searching less-specific locales
  425. * until a non-null non-empty value is found.
  426. *
  427. * @param bundles the stack of bundles to check, most-specific first.
  428. * @param key the key of the value to retrieve.
  429. * @return the first non-null non-empty String found or the last
  430. * retrieved if one isn't found.
  431. */
  432. private static String getString(List<ResourceBundle> bundles, String key)
  433. {
  434. String val = null;
  435. for (ResourceBundle bundle : bundles)
  436. {
  437. val = bundle.getString(key);
  438. if (val != null && !val.isEmpty())
  439. return val;
  440. }
  441. return val;
  442. }
  443. /**
  444. * Retrieves the locale data from the property files and constructs a
  445. * {@code DFSData} instance for it.
  446. *
  447. * @param the locale for which data should be retrieved.
  448. * @return the parsed data.
  449. * @throws MissingResourceException if the resources for the specified
  450. * locale could not be found or loaded.
  451. */
  452. private static DFSData retrieveData(Locale locale)
  453. throws MissingResourceException
  454. {
  455. DFSData data = dataCache.get(locale);
  456. if (data == null)
  457. {
  458. ClassLoader ldr = ClassLoader.getSystemClassLoader();
  459. List<ResourceBundle> bundles = new ArrayList<ResourceBundle>();
  460. ResourceBundle res
  461. = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", locale, ldr);
  462. bundles.add(res);
  463. Locale resLocale = res.getLocale();
  464. while (resLocale != Locale.ROOT)
  465. {
  466. res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
  467. LocaleHelper.getFallbackLocale(resLocale), ldr);
  468. bundles.add(res);
  469. resLocale = res.getLocale();
  470. }
  471. String[] lMonths = getStringArray(bundles, "months", 13);
  472. String[] lWeekdays = getStringArray(bundles, "weekdays", 8);
  473. data = new DFSData(getStringArray(bundles, "ampms", 2),
  474. getStringArray(bundles, "eras", 2),
  475. getString(bundles, "localPatternChars"),
  476. lMonths, getStringArray(bundles, "shortMonths", 13, lMonths),
  477. lWeekdays, getStringArray(bundles, "shortWeekdays", 8, lWeekdays),
  478. formatsForKey(bundles, "DateFormat"),
  479. formatsForKey(bundles, "TimeFormat"),
  480. getZoneStrings(bundles, locale));
  481. DFSData cachedData = dataCache.putIfAbsent(locale, data);
  482. // Use the earlier version if another thread beat us to it.
  483. if (cachedData != null)
  484. data = cachedData;
  485. }
  486. return data;
  487. }
  488. /**
  489. * This method initializes a new instance of <code>DateFormatSymbols</code>
  490. * by loading the date format information for the specified locale.
  491. * This constructor only obtains instances using the runtime's resources;
  492. * to also include {@link java.text.spi.DateFormatSymbolsProvider} instances,
  493. * call {@link #getInstance(java.util.Locale)} instead.
  494. *
  495. * @param locale The locale for which date formatting symbols should
  496. * be loaded.
  497. * @throws MissingResourceException if the resources for the specified
  498. * locale could not be found or loaded.
  499. * @see #getInstance(java.util.Locale)
  500. */
  501. public DateFormatSymbols (Locale locale)
  502. throws MissingResourceException
  503. {
  504. DFSData data = retrieveData(locale);
  505. ampms = data.getAMPMs();
  506. eras = data.getEras();
  507. localPatternChars = data.getLocalPatternChars();
  508. months = data.getMonths();
  509. shortMonths = data.getShortMonths();
  510. weekdays = data.getWeekdays();
  511. shortWeekdays = data.getShortWeekdays();
  512. dateFormats = data.getDateFormats();
  513. timeFormats = data.getTimeFormats();
  514. runtimeZoneStrings = data.getZoneStrings();
  515. }
  516. /**
  517. * This method loads the format symbol information for the default
  518. * locale. This constructor only obtains instances using the runtime's resources;
  519. * to also include {@link java.text.spi.DateFormatSymbolsProvider} instances,
  520. * call {@link #getInstance()} instead.
  521. *
  522. * @throws MissingResourceException if the resources for the default
  523. * locale could not be found or loaded.
  524. * @see #getInstance()
  525. */
  526. public DateFormatSymbols()
  527. throws MissingResourceException
  528. {
  529. this (Locale.getDefault());
  530. }
  531. /**
  532. * This method returns the list of strings used for displaying AM or PM.
  533. * This is a two element <code>String</code> array indexed by
  534. * <code>Calendar.AM</code> and <code>Calendar.PM</code>
  535. *
  536. * @return The list of AM/PM display strings.
  537. */
  538. public String[] getAmPmStrings()
  539. {
  540. return ampms;
  541. }
  542. /**
  543. * This method returns the list of strings used for displaying eras
  544. * (e.g., "BC" and "AD"). This is a two element <code>String</code>
  545. * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
  546. *
  547. * @return The list of era disply strings.
  548. */
  549. public String[] getEras()
  550. {
  551. return eras;
  552. }
  553. /**
  554. * This method returns the pattern character information for this
  555. * object. This is an 18 character string that contains the characters
  556. * that are used in creating the date formatting strings in
  557. * <code>SimpleDateFormat</code>. The following are the character
  558. * positions in the string and which format character they correspond
  559. * to (the character in parentheses is the default value in the US English
  560. * locale):
  561. * <p>
  562. * <ul>
  563. * <li>0 - era (G)</li>
  564. * <li>1 - year (y)</li>
  565. * <li>2 - month (M)</li>
  566. * <li>3 - day of month (d)</li>
  567. * <li>4 - hour out of 12, from 1-12 (h)</li>
  568. * <li>5 - hour out of 24, from 0-23 (H)</li>
  569. * <li>6 - minute (m)</li>
  570. * <li>7 - second (s)</li>
  571. * <li>8 - millisecond (S)</li>
  572. * <li>9 - date of week (E)</li>
  573. * <li>10 - date of year (D)</li>
  574. * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
  575. * <li>12 - week in year (w)</li>
  576. * <li>13 - week in month (W)</li>
  577. * <li>14 - am/pm (a)</li>
  578. * <li>15 - hour out of 24, from 1-24 (k)</li>
  579. * <li>16 - hour out of 12, from 0-11 (K)</li>
  580. * <li>17 - time zone (z)</li>
  581. * </ul>
  582. *
  583. * @return The format patter characters
  584. */
  585. public String getLocalPatternChars()
  586. {
  587. return localPatternChars;
  588. }
  589. /**
  590. * This method returns the list of strings used for displaying month
  591. * names (e.g., "January" and "February"). This is a thirteen element
  592. * string array indexed by <code>Calendar.JANUARY</code> through
  593. * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
  594. * elements because some calendars have thriteen months.
  595. *
  596. * @return The list of month display strings.
  597. */
  598. public String[] getMonths ()
  599. {
  600. return months;
  601. }
  602. /**
  603. * This method returns the list of strings used for displaying abbreviated
  604. * month names (e.g., "Jan" and "Feb"). This is a thirteen element
  605. * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
  606. * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
  607. * elements because some calendars have thirteen months.
  608. *
  609. * @return The list of abbreviated month display strings.
  610. */
  611. public String[] getShortMonths ()
  612. {
  613. return shortMonths;
  614. }
  615. /**
  616. * This method returns the list of strings used for displaying abbreviated
  617. * weekday names (e.g., "Sun" and "Mon"). This is an eight element
  618. * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
  619. * through <code>Calendar.SATURDAY</code>. Note that the first element
  620. * of this array is ignored.
  621. *
  622. * @return This list of abbreviated weekday display strings.
  623. */
  624. public String[] getShortWeekdays ()
  625. {
  626. return shortWeekdays;
  627. }
  628. /**
  629. * This method returns the list of strings used for displaying weekday
  630. * names (e.g., "Sunday" and "Monday"). This is an eight element
  631. * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
  632. * through <code>Calendar.SATURDAY</code>. Note that the first element
  633. * of this array is ignored.
  634. *
  635. * @return This list of weekday display strings.
  636. */
  637. public String[] getWeekdays ()
  638. {
  639. return weekdays;
  640. }
  641. /**
  642. * This method returns this list of localized timezone display strings.
  643. * This is a two dimensional <code>String</code> array where each row in
  644. * the array contains five values:
  645. * <P>
  646. * <ul>
  647. * <li>0 - The non-localized time zone id string.</li>
  648. * <li>1 - The long name of the time zone (standard time).</li>
  649. * <li>2 - The short name of the time zone (standard time).</li>
  650. * <li>3 - The long name of the time zone (daylight savings time).</li>
  651. * <li>4 - the short name of the time zone (daylight savings time).</li>
  652. * </ul>
  653. * <p>
  654. * If {@link #setZoneStrings(String[][])} has been called, then the value
  655. * passed to this will be returned. Otherwise the returned array contains
  656. * zone names provided by the runtime environment and any
  657. * {@link java.util.spi.TimeZoneProvider} instances.
  658. * </p>
  659. *
  660. * @return The list of time zone display strings.
  661. * @see #setZoneStrings(String[][])
  662. */
  663. public String[][] getZoneStrings()
  664. {
  665. if (zoneStrings != null)
  666. return zoneStrings;
  667. return runtimeZoneStrings;
  668. }
  669. /**
  670. * This method sets the list of strings used to display AM/PM values to
  671. * the specified list.
  672. * This is a two element <code>String</code> array indexed by
  673. * <code>Calendar.AM</code> and <code>Calendar.PM</code>
  674. *
  675. * @param value The new list of AM/PM display strings.
  676. */
  677. public void setAmPmStrings (String[] value)
  678. {
  679. if(value==null)
  680. throw new NullPointerException();
  681. ampms = value;
  682. }
  683. /**
  684. * This method sets the list of strings used to display time eras to
  685. * to the specified list.
  686. * This is a two element <code>String</code>
  687. * array indexed by <code>Calendar.BC</code> and <code>Calendar.AD</code>.
  688. *
  689. * @param labels The new list of era display strings.
  690. */
  691. public void setEras (String[] labels)
  692. {
  693. if(labels==null)
  694. throw new NullPointerException();
  695. eras = labels;
  696. }
  697. /**
  698. * This method sets the list of characters used to specific date/time
  699. * formatting strings.
  700. * This is an 18 character string that contains the characters
  701. * that are used in creating the date formatting strings in
  702. * <code>SimpleDateFormat</code>. The following are the character
  703. * positions in the string and which format character they correspond
  704. * to (the character in parentheses is the default value in the US English
  705. * locale):
  706. * <p>
  707. * <ul>
  708. * <li>0 - era (G)</li>
  709. * <li>1 - year (y)</li>
  710. * <li>2 - month (M)</li>
  711. * <li>3 - day of month (d)</li>
  712. * <li>4 - hour out of 12, from 1-12 (h)</li>
  713. * <li>5 - hour out of 24, from 0-23 (H)</li>
  714. * <li>6 - minute (m)</li>
  715. * <li>7 - second (s)</li>
  716. * <li>8 - millisecond (S)</li>
  717. * <li>9 - date of week (E)</li>
  718. * <li>10 - date of year (D)</li>
  719. * <li>11 - day of week in month, eg. "4th Thur in Nov" (F)</li>
  720. * <li>12 - week in year (w)</li>
  721. * <li>13 - week in month (W)</li>
  722. * <li>14 - am/pm (a)</li>
  723. * <li>15 - hour out of 24, from 1-24 (k)</li>
  724. * <li>16 - hour out of 12, from 0-11 (K)</li>
  725. * <li>17 - time zone (z)</li>
  726. * </ul>
  727. *
  728. * @param chars The new format pattern characters
  729. */
  730. public void setLocalPatternChars (String chars)
  731. {
  732. if(chars==null)
  733. throw new NullPointerException();
  734. localPatternChars = chars;
  735. }
  736. /**
  737. * This method sets the list of strings used to display month names.
  738. * This is a thirteen element
  739. * string array indexed by <code>Calendar.JANUARY</code> through
  740. * <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
  741. * elements because some calendars have thriteen months.
  742. *
  743. * @param labels The list of month display strings.
  744. */
  745. public void setMonths (String[] labels)
  746. {
  747. if(labels==null)
  748. throw new NullPointerException();
  749. months = labels;
  750. }
  751. /**
  752. * This method sets the list of strings used to display abbreviated month
  753. * names.
  754. * This is a thirteen element
  755. * <code>String</code> array indexed by <code>Calendar.JANUARY</code>
  756. * through <code>Calendar.UNDECEMBER</code>. Note that there are thirteen
  757. * elements because some calendars have thirteen months.
  758. *
  759. * @param labels The new list of abbreviated month display strings.
  760. */
  761. public void setShortMonths (String[] labels)
  762. {
  763. if(labels==null)
  764. throw new NullPointerException();
  765. shortMonths = labels;
  766. }
  767. /**
  768. * This method sets the list of strings used to display abbreviated
  769. * weekday names.
  770. * This is an eight element
  771. * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
  772. * through <code>Calendar.SATURDAY</code>. Note that the first element
  773. * of this array is ignored.
  774. *
  775. * @param labels This list of abbreviated weekday display strings.
  776. */
  777. public void setShortWeekdays (String[] labels)
  778. {
  779. if(labels==null)
  780. throw new NullPointerException();
  781. shortWeekdays = labels;
  782. }
  783. /**
  784. * This method sets the list of strings used to display weekday names.
  785. * This is an eight element
  786. * <code>String</code> array indexed by <code>Calendar.SUNDAY</code>
  787. * through <code>Calendar.SATURDAY</code>. Note that the first element
  788. * of this array is ignored.
  789. *
  790. * @param labels This list of weekday display strings.
  791. */
  792. public void setWeekdays (String[] labels)
  793. {
  794. if(labels==null)
  795. throw new NullPointerException();
  796. weekdays = labels;
  797. }
  798. /**
  799. * This method sets the list of display strings for time zones.
  800. * This is a two dimensional <code>String</code> array where each row in
  801. * the array contains five values:
  802. * <P>
  803. * <ul>
  804. * <li>0 - The non-localized time zone id string.</li>
  805. * <li>1 - The long name of the time zone (standard time).</li>
  806. * <li>2 - The short name of the time zone (standard time).</li>
  807. * <li>3 - The long name of the time zone (daylight savings time).</li>
  808. * <li>4 - the short name of the time zone (daylight savings time).</li>
  809. * </ul>
  810. *
  811. * @params zones The list of time zone display strings.
  812. */
  813. public void setZoneStrings (String[][] zones)
  814. {
  815. if(zones==null)
  816. throw new NullPointerException();
  817. zoneStrings = zones;
  818. }
  819. /* Does a "deep" equality test - recurses into arrays. */
  820. private static boolean equals (Object x, Object y)
  821. {
  822. if (x == y)
  823. return true;
  824. if (x == null || y == null)
  825. return false;
  826. if (! (x instanceof Object[]) || ! (y instanceof Object[]))
  827. return x.equals(y);
  828. Object[] xa = (Object[]) x;
  829. Object[] ya = (Object[]) y;
  830. if (xa.length != ya.length)
  831. return false;
  832. for (int i = xa.length; --i >= 0; )
  833. {
  834. if (! equals(xa[i], ya[i]))
  835. return false;
  836. }
  837. return true;
  838. }
  839. private static int hashCode (Object x)
  840. {
  841. if (x == null)
  842. return 0;
  843. if (! (x instanceof Object[]))
  844. return x.hashCode();
  845. Object[] xa = (Object[]) x;
  846. int hash = 0;
  847. for (int i = 0; i < xa.length; i++)
  848. hash = 37 * hashCode(xa[i]);
  849. return hash;
  850. }
  851. /**
  852. * This method tests a specified object for equality against this object.
  853. * This will be true if and only if the specified object:
  854. * <p>
  855. * <ul>
  856. * <li> Is not <code>null</code>.</li>
  857. * <li> Is an instance of <code>DateFormatSymbols</code>.</li>
  858. * <li> Contains identical formatting symbols to this object.</li>
  859. * </ul>
  860. *
  861. * @param obj The <code>Object</code> to test for equality against.
  862. *
  863. * @return <code>true</code> if the specified object is equal to this one,
  864. * <code>false</code> otherwise.
  865. */
  866. public boolean equals (Object obj)
  867. {
  868. if (! (obj instanceof DateFormatSymbols))
  869. return false;
  870. DateFormatSymbols other = (DateFormatSymbols) obj;
  871. return (equals(ampms, other.ampms)
  872. && equals(eras, other.eras)
  873. && equals(localPatternChars, other.localPatternChars)
  874. && equals(months, other.months)
  875. && equals(shortMonths, other.shortMonths)
  876. && equals(shortWeekdays, other.shortWeekdays)
  877. && equals(weekdays, other.weekdays)
  878. && equals(zoneStrings, other.zoneStrings));
  879. }
  880. /**
  881. * Returns a new copy of this object.
  882. *
  883. * @return A copy of this object
  884. */
  885. public Object clone ()
  886. {
  887. try
  888. {
  889. return super.clone ();
  890. }
  891. catch (CloneNotSupportedException e)
  892. {
  893. return null;
  894. }
  895. }
  896. /**
  897. * This method returns a hash value for this object.
  898. *
  899. * @return A hash value for this object.
  900. */
  901. public int hashCode ()
  902. {
  903. return (hashCode(ampms)
  904. ^ hashCode(eras)
  905. ^ hashCode(localPatternChars)
  906. ^ hashCode(months)
  907. ^ hashCode(shortMonths)
  908. ^ hashCode(shortWeekdays)
  909. ^ hashCode(weekdays)
  910. ^ hashCode(zoneStrings));
  911. }
  912. /**
  913. * Returns a {@link DateFormatSymbols} instance for the
  914. * default locale obtained from either the runtime itself
  915. * or one of the installed
  916. * {@link java.text.spi.DateFormatSymbolsProvider} instances.
  917. * This is equivalent to calling
  918. * <code>getInstance(Locale.getDefault())</code>.
  919. *
  920. * @return a {@link DateFormatSymbols} instance for the default
  921. * locale.
  922. * @since 1.6
  923. */
  924. public static final DateFormatSymbols getInstance()
  925. {
  926. return getInstance(Locale.getDefault());
  927. }
  928. /**
  929. * Returns a {@link DateFormatSymbols} instance for the
  930. * specified locale obtained from either the runtime itself
  931. * or one of the installed
  932. * {@link java.text.spi.DateFormatSymbolsProvider} instances.
  933. *
  934. * @param locale the locale for which an instance should be
  935. * returned.
  936. * @return a {@link DateFormatSymbols} instance for the specified
  937. * locale.
  938. * @throws NullPointerException if <code>locale</code> is
  939. * <code>null</code>.
  940. * @since 1.6
  941. */
  942. public static final DateFormatSymbols getInstance(Locale locale)
  943. {
  944. try
  945. {
  946. DateFormatSymbols syms = new DateFormatSymbols(locale);
  947. return syms;
  948. }
  949. catch (MissingResourceException e)
  950. {
  951. /* This means runtime support for the locale
  952. * is not available, so we check providers. */
  953. }
  954. for (DateFormatSymbolsProvider p :
  955. ServiceLoader.load(DateFormatSymbolsProvider.class))
  956. {
  957. for (Locale loc : p.getAvailableLocales())
  958. {
  959. if (loc.equals(locale))
  960. {
  961. DateFormatSymbols syms = p.getInstance(locale);
  962. if (syms != null)
  963. return syms;
  964. break;
  965. }
  966. }
  967. }
  968. return getInstance(LocaleHelper.getFallbackLocale(locale));
  969. }
  970. }