Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Calendar.getInstance() and GregorianCalendar.getInstance()

Tags:

java

calendar

Ever since it was first introduced to replace Date I have been using the static method Calendar.getInstance() to get a new Calendar object. I have never had a problem but I just wondered for some reason if it were better to use the GregorianCalendar.getInstance() method instead.

Is it possible my programs will run in some locale or some JVM somewhere where the superclass version will return an object class I wasn't expecting? Has there ever been an implementation of Calendar other than GregorianCalendar that ever made it into wide use?

As I said I'm not having a problem at the moment but I am always looking to improve my practices.

like image 764
AlanObject Avatar asked Aug 31 '14 16:08

AlanObject


1 Answers

Yes, Calendar could return a locale 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;
}
like image 153
Peter Lawrey Avatar answered Sep 20 '22 17:09

Peter Lawrey