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

别来无恙 提交于 2019-12-03 05:30:41

问题


What might be the difference between getting datetime using

Calendar.getInstance()

vs

new GregorianCalendar()

?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6905288/getting-current-datetime-using-calendar-getinstance-vs-new-gregoriancalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!