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

Find the date for the Chinese New Year

Problem:

You want to find the date of the Chinese new year for a given year.

Solution:

Use icu4j's Chinese calendar to perform the calculations.  You will specify a year in a Gregorian calendar and use that to set the time of the Chinese calendar.  Then set the calendar tot he first month and first day.

//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 Gregorian calendar instance
Calendar c2 = Calendar.getInstance();
//Set the year you want to find new Years for.  We will also set the month
//to half way through the year since the Chinese New Year won't occur until perhaps late February
c2.set(Calendar.YEAR, 2008);
c2.set(Calendar.MONTH, 5);
//Set the year of the Chinese Calendar
c.setTimeInMillis(c2.getTimeInMillis());
//Set the month to the first month.  Remember months are zero based
c.set(Calendar.MONTH, 0);
//Set the day to the first.  Remember dates are one based.
c.set(Calendar.DATE, 1);
//Output the date.  getTime() returns a Gregorian date.
System.out.println(c.getTime());

The output:

Thu Feb 07 22:35:16 EST 2008 Shows us the New Years occured on Feb 7th in 2008.  The time output is irrelevant.

 Discussion:

The length of the Chinese calendar's year varies from year to year.  Since the Calendar uses a leap month, the New Year can occur anywhere from late January to late February. 

 

‹ Get an array of Holidays up Get all the Era names for the Japanese Calendar ›
  • calendar
  • chinese calendar
  • 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