Recipe 6.3 Representing Dates in Other Epochs


Problem

You need to deal with dates in a form other than the Gregorian Calendar used in the Western world.

Solution

Download the IBM calendar classes.

Discussion

The only nonabstract Calendar subclass is the GregorianCalendar, as mentioned previously. However, other calendar classes do exist. IBM has a set of calendars Hebrew, Islamic, Buddhist, Japanese, and even an Astronomical Calendar class that covers most of the rest of the world. This work has been open sourced and is now part of a project called International Components for Unicode for Java, which can be found at http://oss.software.ibm.com/icu4j/.

The calendar classes in ICU4J work in a similar fashion to the standard GregorianCalendar class, but they have constants for month names and other information relevant to each particular calendar. They are not subclassed from java.util.Calendar, however, so they must be referred to by their full class name:

import java.util.Locale; import com.ibm.icu.util.Calendar; import java.text.DateFormat; import com.ibm.icu.util.IslamicCalendar; public class IBMCalDemo {         public static void main(String[] args) {                 Locale ar_loc = new Locale("ar");                 Calendar c = new com.ibm.icu.util.IslamicCalendar( );                 DateFormat d = DateFormat.getDateInstance(DateFormat.LONG, ar_loc);                 System.out.println(d.format(c.getTime( )));         } }

I can't include the textual output because of font limitations. The icu4j package includes a multicalendar demo application whose output is shown in Figure 6-1.

Figure 6-1. IBMCalDemo in action
figs/jcb2_0601.gif




Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net