Java Cookbook
icu4j
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.
Problem:
You want to create a Chinese calendar.
Solution:
Use icu4j to create a calendar instance.
Problem:
You want to know what holidays are common for a locale.
Solution:
Icu4j contains a system to help the Java developer handle international holidays. The Holiday class is simple to use, and has failry complete data.
To get an Array of all Holidays for Mexico:
//Get a ULocale
ULocale locale = new ULocale("es_MX");
//Get an array of Holidays
Holiday[] holidays = Holiday.getHolidays(locale);
Problem:
You want to know what character set a web page is encoded in.
Solution:
The Java programmer can use icu4j's CharsetDetector class to create a best guess for the charset of a specified input stream. Unfortunately this is very much a guess, and should not be overly relied upon.
To get the Charset of web page:
Problem:
You want to spell out a dynamic numeric value being combined with a static resource.
Solution:
Java MessageFormat provides a number of options for formatting the values passed in. ICU4J provides even more options.
One of the more interesting formatting options is the spellout format. Spell out will actually write out a numeric value, such as thirty-five vs. 35.
ICU4J's MessageFormat is used similar to the code Java class.
To format a pattern using a spellout pattern:
Problem:
You want to format an ordinal number like "1st" or "2nd".
Solution:
Java programmers can handle ordinal number formatting by leveraging ICU4J's RuleBasedNumberFormat class.
An ordinal number is a number like "1st" "2nd" etc.
To format an ordinal number:
//Get a RuleBasedNumberFormat appropriate for English ordinal format
Problem:
You want to spell out a localized number like "thirty-six."
Solution:
ICU4J provides some number formatting capabilities that core Java does not. One of those features is the ability to spell out a numeric value.
Problem:
You want more options and power than provided by a core Java locale.
Solution:
ICU4J provides a locale class that provides significantly more capabilites than the Locale class in core Java. ULocale is the foundation of all internationalization classes in ICU4J.
ULocale is defined with at minimum a language code. It can also contain script name, region code, and other locale specific meta data such as calendar type.
To get an English ULocale:
Problem:
You want to use a Hebrew Calendar in your application.
Solution:
The Hebrew calendar is a very interesting lunar-solar calendar that poses some interesting challenges for the pogrammer. It uses a leap month to synch the lunar and solar components. The first day of the year can vary depending upon how it will affect jewish holidays, and the day ends at sundown.
Problem:
You want a calendar that is most familiar to your Japanese users.
Solution:
The Japanese calendar is identical to the Gregorian calendar, except the year is represented by the year of the emperor's reign. It is currently the 20th year of the Heisei emperor's reign, so the year is Heisei 20.
You can create a Japanese calendar by using icu4j .
To create a calendar object:
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