5.7.  Format text for localization by using NumberFormat and DateFormat


NumberFormat class

By invoking the methods provided by the NumberFormat class, you can format numbers, currencies, and percentages according to Locale.

You can use the NumberFormat methods to format primitive-type numbers, such as double, int and their corresponding wrapper objects, such as Double, Integer.

The following code example formats an int according to Locale. Invoking the getNumberInstance(Locale l) method returns a locale-specific instance of NumberFormat:

Locale l = Locale.US;
NumberFormat formatter = NumberFormat.getNumberInstance(l);

The format(Object o) method accepts the Integer as an argument and returns the formatted number in a String:

Integer i = 15_091_974;
System.out.println(String.format("Locale: %s; int: %s", l, formatter.format(i)));


The output generated by this code follows:

Locale: en_US; int: 15,091,974

If you are writing business applications, you will probably need to format and display currencies. You format currencies in the same manner as numbers, except that you call getCurrencyInstance(Locale l) to create a formatter. When you invoke the format(Object o) method, it returns a String that includes the formatted number and the appropriate currency sign.

This code example shows how to format currency in a locale-specific manner:

Integer i = 15_091_974;
Locale l = Locale.US;
NumberFormat formatter = NumberFormat.getCurrencyInstance(l);
System.out.println(String.format("Locale: %s; currency: %s", l, formatter.format(i)));


The output generated by the preceding lines of code is as follows:

Locale: en_US; currency: $15,091,974.00

DateFormat class

The DateFormat class allows you to format dates and times with predefined styles in a locale-sensitive manner.

Formatting dates with the DateFormat class is a two-step process:

The output generated by this code follows:

Locale: en_US; Date: Apr 5, 2012

SimpleDateFormat class

If you want to create your own customized date formats, you can use the SimpleDateFormat class.

When you create a SimpleDateFormat object, you specify a pattern String. The contents of the pattern String determine the format of the date and time.

The following pattern letters are defined:

G   Era designator                                       AD
y   Year                                                 1996; 96
M   Month in year                                        July; Jul; 07
w   Week in year                                         27
W   Week in month                                        2
D   Day in year                                          189
d   Day in month                                         10
F   Day of week in month                                 2
E   Day name in week                                     Tuesday; Tue
u   Day number of week (1 = Monday, ..., 7 = Sunday)     1
a   Am/pm marker                                         PM
H   Hour in day (0-23)                                   0
k   Hour in day (1-24)                                   24
K   Hour in am/pm (0-11)                                 0
h   Hour in am/pm (1-12)                                 12
m   Minute in hour                                       30
s   Second in minute                                     55
S   Millisecond                                          978
z   Time zone                                            Pacific Standard Time; PST; GMT-08:00
Z   Time zone                                            -0800
X   Time zone                                            -08; -0800; -08:00

The following code formats a date according to the pattern String passed to the SimpleDateFormat constructor. The String returned by the format(...) method contains the formatted date that is to be displayed:

Date d = new Date();
String pattern = "EEE, MMMM d, yyyy";
Locale l = Locale.US;
SimpleDateFormat sdf  = new SimpleDateFormat(pattern, l);
System.out.println(String.format("Locale: %s; custom date format: %s", l, sdf.format(d)));



Locale: en_US; custom date format: Thu, April 5, 2012

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide