java – Calendar. Getinstance() and gregoriancalendar The difference between getinstance()
•
Java
Since I first introduced the replacement date, I have been using the static method calendar GetInstance () to get a new calendar object I've never had any problems, but I just want to know if it's best to use Gregorian calendar GetInstance () method,
Is it possible that my program will run somewhere or somewhere in some JVM, and the super class version will return an object class I don't expect? Is there an executive calendar, rather than Gregorian Carr calendar, that has made it widely used?
As I said, I have no problem at present, but I have been looking for ways to improve my practice
Solution
Yes, the calendar may return a region - specific calendar From the source
/** * Gets a calendar using the default time zone and locale. The * <code>Calendar</code> returned is based on the current time * in the default time zone with the default * {@link Locale.Category#FORMAT FORMAT} locale. * * @return a Calendar. */ public static Calendar getInstance() { return createCalendar(TimeZone.getDefault(),Locale.getDefault(Locale.Category.FORMAT)); } private static Calendar createCalendar(TimeZone zone,Locale aLocale) { CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider.class,aLocale) .getCalendarProvider(); if (provider != null) { try { return provider.getInstance(zone,aLocale); } catch (IllegalArgumentException iae) { // fall back to the default instantiation } } Calendar cal = null; if (aLocale.hasExtensions()) { String caltype = aLocale.getUnicodeLocaleType("ca"); if (caltype != null) { switch (caltype) { case "buddhist": cal = new BuddhistCalendar(zone,aLocale); break; case "japanese": cal = new JapaneseImperialCalendar(zone,aLocale); break; case "gregory": cal = new GregorianCalendar(zone,aLocale); break; } } } if (cal == null) { // If no kNown calendar type is explicitly specified,// perform the Traditional way to create a Calendar: // create a BuddhistCalendar for th_TH locale,// a JapaneseImperialCalendar for ja_JP_JP locale,or // a GregorianCalendar for any other locales. // NOTE: The language,country and variant strings are interned. if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") { cal = new BuddhistCalendar(zone,aLocale); } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja" && aLocale.getCountry() == "JP") { cal = new JapaneseImperialCalendar(zone,aLocale); } else { cal = new GregorianCalendar(zone,aLocale); } } return cal; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码