Chinese Calendar

Problem:

You want to create a Chinese calendar.

Solution:

Use icu4j to create a calendar instance.

//Get a ULocale for Chinese written in simplified Chinese for China using a Chinese calendar
ULocale chinese = new ULocale("zh_Hans_CN@calendar=chinese");
//Get a Calendar instance specifying our ULocale.  This will give us a Chinese calendar
Calendar c = Calendar.getInstance(chinese);
//Get a DateTimeFormat appropriate for the calendar.
DateFormat df = c.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, chinese);
//Output a formatted date.
System.out.println(df.format(new Date()));
//Output the pattern.
System.out.println(((SimpleDateFormat)df).toPattern());

 

Discussion:

The Chinese calendar is one of the most interesting and accurate calendars in use.  It is a lunisolar calendar that introduces a leap month to handle the justification of the lunar and solar systems.  The months are lunar months, but the leap month is added to avoid the calendar diverging from the solar cycle.  The calendar is still in wide use across Asia.

 

The Chinese calendar uses a system of measuring years in 60 year cycles.  The cycles are broken into earthly and heavenly stems of the elements and branches with animal names. 

 

Icu4j offers and excellent implementation of the Chinese calendar. It is very easy to implement.  By default it will output the year as "year x cycle".  I will demonstrate in another recipe how to create a more interesting format.