The i18n Cookbook - recipies for a global society

  • java cookbook
  • about the author
Home

Java Cookbook

  • Java Internationalization Cookbook

icu4j

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.

  • calendar
  • chinese calendar
  • icu4j
  • Add new comment
  • Read more

Chinese Calendar

Problem:

You want to create a Chinese calendar.

Solution:

Use icu4j to create a calendar instance.

  • calendar
  • date
  • icu4j
  • Add new comment
  • Read more

Get an array of Holidays

 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);

  • calendar
  • Holiday
  • icu4j
  • Add new comment
  • Read more

Detect the Charset of a URL

 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:

  • Charset
  • icu4j
  • Add new comment
  • Read more

Spell out the numeric value in a concatenated string

 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:

  • icu4j
  • messageformat
  • spellout
  • Add new comment
  • Read more

Format Ordinal Numbers

 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

  • icu4j
  • number format
  • rulebasednumberformat
  • Add new comment
  • Read more

Spell out a numeric value

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. 

 

  • icu4j
  • number format
  • spellout
  • Add new comment
  • Read more

Create an ICU4J ULocale

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:

  • icu4j
  • Java
  • Locale
  • ulocale
  • Add new comment
  • Read more

Hebrew Calendar

 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.

 

  • calendar
  • hebrew calendar
  • icu4j
  • Java
  • ulocale
  • Add new comment
  • Read more

Japanese Calendar

 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:

  • calendar
  • icu4j
  • japanese calendar
  • Java
  • Add new comment
  • Read more
  • 1
  • 2
  • next ›
  • last »

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
Syndicate content

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