开发者

Java Localized Calendar

I'm developing an internationalized application, in which I need to dynamically change my calendar without changing the code. As you know, Calendar.getInstance() returns a Gregorian calendar, how can I use a different ca开发者_如何学JAVAlendar when I call Calendar.getInstance(). So, I dynamically change my localized calendar without changing the code.

Best


Use the factory to do so:

public static Calendar getInstance(TimeZone zone, Locale aLocale)


If you want to use different calendar system (it is really hard to tell what you are up to), then ICU4J Project is your friend.
It contains several different calendar systems (see com.ibm.icu.util package).

To use different calendar system, just instantiate Calendar with valid ULocale and you are all done:

// valid for desktop applications, for web you'd do it differently
ULocale arabicLocale = new ULocale("ar_SA@calendar=islamic");
Calendar calendar = Calendar.getInstance(arabicLocale);

Unfortunately, I can help it but it seems that you would have to assign calendar system through specific ULocale identifier. I don't know why they haven't set proper defaults... Anyway...

ICU also provides its own implementation of DateFormat as well as TimeZone and I am afraid that this is what you should use to format Calendar. Fortunately their implementation accepts Calendar, so it is actually easier to use:

TimeZone currentTimeZone = TimeZone.getDefault();
DateFormat formatter = DateFormat.getDateTimeInstance(
    DateFormat.DEFAULT, DateFormat.DEFAULT, arabicLocale);
formatter.setTimeZone(currentTimeZone);
String formattedDateTime = formatter.format(calendar);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜