Internationalization and localization tools


Locale-Sensitive Java Method

java.util.DateFormat

public static final DateFormat getInstance()

Internationalization (I18n) Method Overview

DateFormat helps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar. Most of the factory methods allow you to explicitly pass the user's locale as an argument. This factory method does not, and always produces a date/time formatter that formats a date/time string according to the machine's default locale using the SHORT format:

  • dateStyle = M/d/yy if the default locale is US
  • timeStyle = "h:mm a" if the default locale is US

I18n Issues

The default machine locale is not always the correct locale for every user. Hence, it is recommended I18n practice to call the DateFormat.getDateTimeInstance method so you can explicitly pass the user's locale and produce a date/time format that is independent of the machine's default locale.

Suggested Replacement

public static final DateFormat getDateTimeInstance(
   int dateStyle,
   int timeStyle,
   Locale aLocale)

Instead of:

DateFormat dateFormatter =
  DateFormat.getInstance();

Use:

Locale locale = getUserLocale();
DateFormat dateFormatter =
  DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
  DateFormat.DEFAULT,
  locale);

Please see Times and Dates for more information.

Locale-Sensitive Java Methods

 

Lingoport internationalization and localization services and software