- 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
Get all the Era names for the Japanese Calendar
Problem:
You want to retrieve all of the era names for the Japanese calendar.
Solution:
Use the icu4j Japanese calendar and DateFormatSymbols to retrieve the era names.
//Get a ULocale that specifies a Japanese Calendar in Japanese
ULocale jp = new ULocale("ja_JP@calendar=japanese");
//Get a Japanese calendar instance
Calendar jc = Calendar.getInstance(jp);
//The era field contains the numeric identifier for the reigning emperor at the current time
System.out.println(jc.get(Calendar.ERA));
//Get an instance of the DateFormatSymbols for Japanese using the Japanese calendar
DateFormatSymbols dfs = DateFormatSymbols.getInstance(jp);
//getEras() returns a String array of era names
String[] eras = dfs.getEras();
//Loop through the eras and output
for(int x = 0; x < eras.length; x++){
System.out.println(eras[x]);
}
//Now let's get the English Era names
//Get a ULocale that specifies a Japanese Calendar in English
ULocale en = new ULocale("en@calendar=japanese");
//Get the DateFormatSymbols for English using a Japanese Calendar
dfs = DateFormatSymbols.getInstance(en);
eras = dfs.getEras();
for(int x = 0; x < eras.length; x++){
System.out.println(eras[x]);
}
Discussion:
The Japanese calendar counts years based upon the reign of the emperor. Since the reign of an emperor varies in length it is impossible to calculate the year programmatically. icu4j's DateFormatSymbols can provide us with a list of all Emperors. To find the Japanese year for a Gregorian date use this recipe.
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