Getting current datetime using Calendar.getInstance() vs new GregorianCalendar()

后端 未结 2 361
忘了有多久
忘了有多久 2021-02-01 01:30

What might be the difference between getting datetime using

Calendar.getInstance()

vs

new GregorianCalendar()

?

相关标签:
2条回答
  • 2021-02-01 02:11

    Calendar.getInstance() will give you a Calendar using the default time zone and locale, which can result in a GregorianCalendar, a BuddhistCalendar, or a JapaneseImperialCalendar.

    GregorianCalendar will always give you, well, a Gregorian calendar.

    0 讨论(0)
  • 2021-02-01 02:25

    Looking in the source of Calendar.getInstance():

    private static Calendar createCalendar(TimeZone zone, Locale aLocale) {
        // If the specified locale is a Thai locale, returns a BuddhistCalendar
        // instance.
        if ("th".equals(aLocale.getLanguage())
            && ("TH".equals(aLocale.getCountry()))) {
            return new sun.util.BuddhistCalendar(zone, aLocale);
        } else if ("JP".equals(aLocale.getVariant())
            && "JP".equals(aLocale.getCountry())
            && "ja".equals(aLocale.getLanguage())) {
            return new JapaneseImperialCalendar(zone, aLocale);
        }       
    
        // else create the default calendar
        return new GregorianCalendar(zone, aLocale);    
    }
    

    So getInstance() will return a Calendar based on your default Locale and TimeZone.

    0 讨论(0)
提交回复
热议问题