日期:2014-05-20 浏览次数:20855 次
public static Calendar getInstance() { Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)); cal.sharedZone = true; return cal; } public static Calendar getInstance(TimeZone zone, Locale aLocale) { return createCalendar(zone, aLocale); } public static Calendar getInstance(TimeZone zone) { return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT)); } public static Calendar getInstance(Locale aLocale) { Calendar cal = createCalendar(TimeZone.getDefaultRef(), aLocale); cal.sharedZone = true; return cal; } private static Calendar createCalendar(TimeZone zone, Locale aLocale) { Calendar cal = null; String caltype = aLocale.getUnicodeLocaleType("ca"); if (caltype == null) { // Calendar type is not specified. // If the specified locale is a Thai locale, // returns a BuddhistCalendar instance. if ("th".equals(aLocale.getLanguage()) && ("TH".equals(aLocale.getCountry()))) { cal = new BuddhistCalendar(zone, aLocale); } else { cal = new GregorianCalendar(zone, aLocale); } } else if (caltype.equals("japanese")) { cal = new JapaneseImperialCalendar(zone, aLocale); } else if (caltype.equals("buddhist")) { cal = new BuddhistCalendar(zone, aLocale); } else { // Unsupported calendar type. // Use Gregorian calendar as a fallback. cal = new GregorianCalendar(zone, aLocale); } return cal; }
------解决方案--------------------
class A{
private static A a=null; //创建本对象的引用,私有,不可以直接调用
private A(){} //创建私有的构造方法,不可以直接调用初始化类
public static A getA(){ //初始化类的方法
if(a==null){ //如果a还没有初始化,就初始化该类
a=new A();
}
return a;
}
}