Java Cookbook
Java
Problem:
You want to know what time zones are available on your system.
Solution:
Java uses the TimeZone class to handle date/time casting to and from UTC. A TimeZone is retrieved by specifying a string id. Most of these ids are Olsen time zone ids. You can retrieve an array of time zone ids by calling the static getAvailableIDs() method.
To get all available IDs:
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 convert the script text is written in.
Solution:
The Transliterator from icu4j class allows for easy conversion of text from one writing system to another. To transliterate text you simply get an instance of the Transliterator class using the id of the writing system you are converting from, and the id of the system you want to convert into, and then call the transliterate method on the object.
Problem:
You have a source id but you want to retrieve all available target ids for the source id.
Solution:
A Transliterator is retrieved using an id that is a combination of a source and target id.
Problem:
You want to retireve all available source ids for a Transliterator.
Solution:
A Transliterator is created using a combination of a source and a target id joined by a hyphen.
To get a list of all sources:
Problem:
You want to get all available transliterator ids.
Solution:
You obtain a Transliterator by specifying an id. The id is a String that contains two script names separated by a hyphen, for example: "Latin-Hangul."
To obtain a list of all ids:
Problem:
You want to parse a localized date string to a Java Date.
Solution:
As important as date formatting is, you have to be able to parse the formatted dates. Parsing a date is very similar to formatting the date. You just call the parse method on a DateFormat instance.
Problem:
You want to retrieve all of the day of the week names for a locale.
Solution:
The DateFormatSymbols class contains a wealth of information useful to the localization programmer. You can easily retrieve day names, month names, era names, etc...
To get the days of the week in Korean and English:
//Get a DateFormatSymbols object using a locale as an argument
DateFormatSymbols dfsKorean = new DateFormatSymbols(Locale.KOREAN);
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