java – Calendar.getInstance()和GregorianCalendar.getInstance(
发布时间:2020-05-23 03:06:26 所属栏目:Java 来源:互联网
导读:自从第一次引入替换Date以来,我一直在使用静态方法Calendar.getInstance()来获取一个新的Calendar对象.我从来没有遇到任何问题,但我只是想知道如果最好使用GregorianCalendar.getInstance()方法, 是否可能我的程序将运行在某些地方或某些JVM的某个地方超类版
|
自从第一次引入替换Date以来,我一直在使用静态方法Calendar.getInstance()来获取一个新的Calendar对象.我从来没有遇到任何问题,但我只是想知道如果最好使用GregorianCalendar.getInstance()方法, 是否可能我的程序将运行在某些地方或某些JVM的某个地方超类版本将返回一个我不期望的对象类?有没有一个执行日历,而不是格雷戈里安卡尔日历,曾经使它广泛使用? 正如我所说,我目前没有问题,但我一直在寻求改善我的做法. 解决方法是的,日历可能会返回特定于地区的日历.从源头./**
* 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;
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
