The i18n Cookbook - recipies for a global society

  • java cookbook
  • about the author
Home › Java Internationalization Cookbook › Dates and Times › Calendars

Java Cookbook

  • Java Internationalization Cookbook
    • Locales
    • Dates and Times
      • Calendars
        • Gregorian Calendar
        • Hebrew Calendar
        • Japanese Calendar
        • Chinese Calendar
        • Find the Chinese zodiac for a Gregorian year
        • Get the name of the current month
        • Get the first day of the week
        • Add time to Calendar
        • Get an array of Holidays
        • Find the date for the Chinese New Year
        • Get all the Era names for the Japanese Calendar
        • Get the Japanese era for a Gregorian date
      • Formating dates and times
    • Numerical Systems
    • Misc
    • Resource Bundles
    • Unicode, Transliteration, and Charactersets

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.

‹ Japanese Calendar up Find the Chinese zodiac for a Gregorian year ›
  • calendar
  • date
  • icu4j
  • Printer-friendly version
  • Add new comment

If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.

This site is ad supported.  I hope you find something among our sponsors worth clicking. ;)

i18n search

Google
Custom Search

Search

Tags in Tags

calendar date icu4j Java Locale number format numberformat parse spellout timezone transliteration transliterator
more tags

User login

  • Create new account
  • Request new password
  • java cookbook
  • about the author