Java Calendar.getInstance()和GregorianCalendar.getInstance()之间的差异
自从第一次引入它来替换日期以来,我一直在使用静态方法Calendar.getInstance()来获取一个新的Calendar对象。我从来没有遇到过问题,但我只是出于某种原因想知道使用gregoriacalendar.getInstance()方法是否更好 我的程序是否可能在某个区域设置或某个JVM中运行,其中超类版本将返回我不期望的对象类?除了GregorianCalendar之外,是否有其他的Calendar的实现使其得到广泛使用Java Calendar.getInstance()和GregorianCalendar.getInstance()之间的差异,java,calendar,Java,Calendar,自从第一次引入它来替换日期以来,我一直在使用静态方法Calendar.getInstance()来获取一个新的Calendar对象。我从来没有遇到过问题,但我只是出于某种原因想知道使用gregoriacalendar.getInstance()方法是否更好 我的程序是否可能在某个区域设置或某个JVM中运行,其中超类版本将返回我不期望的对象类?除了GregorianCalendar之外,是否有其他的Calendar的实现使其得到广泛使用 正如我所说,我现在没有问题,但我一直在寻求改进我的练习 是,
正如我所说,我现在没有问题,但我一直在寻求改进我的练习 是,日历可以返回特定于区域设置的日历。从源头上
/**
* 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;
}
在泰国,佛历是首选,将通过
calendar.getInstance()
返回。如果您想确保获得公历,您应该始终使用该类的构造函数,而不是静态工厂方法getInstance()
。Android中不提供BuddhistCalendar类。你能帮我从哪里上这门课以及如何设置日期和时间吗?@Pratik我不知道。@Pratik Android!=Java,另请参见Android上的非公历日历