- 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
- Calendars
- Numerical Systems
- Misc
- Resource Bundles
- Unicode, Transliteration, and Charactersets
Java Cookbook
Find the Chinese zodiac for a Gregorian year
Problem:
You want to know the Chinese zodiac sign for a Gregorian year.
Solution:
Use the icu4j Chinese calendar to get the appropriate branch. The following code illustrates and example:
//An array of heavenly and earthly elements. The elements start with wood and end with
//water, but since we are using a modulus to determine the element we have moved earthly water
//to position 0
String[] enElements = {"Water","Wood","Wood","Fire","Fire","Earth",
"Earth","Metal","Metal","Water"};
String[] zhElements = {"癸","甲","乙","丙","丁","戊","己","庚","辛","壬"};
//The 12 animals of the Chinese calendar. Rat is first and Boar is last but we have
//moved boar to position zero so our modulus returns a correct result.
String[] enAnimals = {"Boar","Rat","Ox","Tiger","Rabbit","Dragon","Snake",
"Horse","Sheep","Monkey","Rooster","Dog"};
String[] zhAnimals = {"亥","子","丑","寅","卯","辰","巳",
"午","未","申","酉","戌"};
//Get an instance of a Chinese calendar by specifying a calendar type of Chinese on our ULocale
ULocale chinese = new ULocale("zh_Hans_CN@calendar=chinese");
Calendar c = Calendar.getInstance(chinese);
//Get a Gregorian calendar instance for setting our dates
Calendar g = Calendar.getInstance();
//We will set the year, month, and day since the stem and branch change on
//Chinese New Year, not January 1st.
//Set the year
g.set(Calendar.YEAR, 2008);
//Set the month. Remember the month is 0 based so January is 0 and December is 11.
g.set(Calendar.MONTH, 0);
//Set the day of the month
g.set(Calendar.DATE, 7);
//Output the date to confirm
System.out.println(DateFormat.getDateInstance().format(g.getTime()));
//Set the Chinese calendar for your specified date
c.setTimeInMillis(g.getTimeInMillis());
//Get the element stem. The current year mod 10 will give us a value between 0 and 9.
//We use that value to retrieve the element from the elements array.
String enElement = enElements[c.get(Calendar.YEAR)%10];
String zhElement = zhElements[c.get(Calendar.YEAR)%10];
//Get the animal branch. The current year mod 12 will give us a value between 0 and 11.
//We use that value to retrieve the animal from the animals array.
String enAnimal = enAnimals[c.get(Calendar.YEAR)%12];
String zhAnimal = zhAnimals[c.get(Calendar.YEAR)%12];
System.out.println(enElement + " " + enAnimal);
System.out.println(zhElement + zhAnimal);
Discussion:
The Chinese Calendar uses a 60 year cycle. The year is represented by a branch representing heavenly and earthly examples for the 5 elements and the 12 animals of the zodiac. The zodiac changes based upon the Chinese New Year, so the calculations are little more complex and require the month and day to be trully accurate.
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
Custom Search