This section provides a tutorial example on how to convert date objects to strings in different locales using java.util.SimpleDateFormat class.
Locale Sensitive Operations: Operations that require to know a user's locale
in order to tailor information for the user. For example, formatting a calendar date
into a string is a locale sensitive operation, because for an English speaking user
in US, a calendar date should be formatted as "Mon Oct 21 20:54:05 EDT 2002"; but
for a French speaking user, the same date should be formatted as "lun. oct. 21
20:54:05 EDT 2002".
Separated formatter classes are provided to handle locale sensitive operations
related to some predefined classes. Here is a list of formatter classes:
Formatter Data Operations
Class Class
SimpleDateFormat GregorianCalendar Formating and parsing
DecimalFormat Number Formating and parsing
The following program shows how to internationalize the formatting operation of
dates, numbers and currencies:
Default date format:
Locale(Default): November 1, 2002 9:45:44 AM EST
Locale(France): 1 novembre 2002 09:45:44 EST
Locale(French): 1 novembre 2002 09:45:44 EST
Locale(German): 1. November 2002 09:45:44 EST
Customized date format:
Locale(Default): Fri Nov 01 09:45:44 EST 2002
Locale(French): ven. nov. 01 09:45:44 EST 2002
Locale(German): Fr Nov 01 09:45:44 EST 2002
Locale(Chinese): ??? ??? 01 09:45:44 EST 2002
Number format:
Locale(Default): -1,234.568
Locale(France): -1á234,568
Locale(French): -1á234,568
Locale(Japanese): -1,234.568
Currency format:
Locale(Default): ($1,234.57)
Locale(France): -1á234,57 F
Locale(French): -1á234,57 ñ
Locale(Japanese): -?1,235
Note that:
When the locale is changed, the default formatter not only changes the language,
but also changes the pattern.
The "?" in the output of Chinese locale is caused by the console window not able
to display the Chinese characters.
The currency formatter is sensitive to not only the language, but also to the country.
The precision is also adjusted in the default currency formatter for Japanese.