Internationalization and localization tools


Locale-Sensitive Java Method

java.util.DateFormat

public static final DateFormat getDateInstance()

public static final DateFormat getDateInstance(int style)

public static final DateFormat getDateInstance(int style,
   Locale aLocale)

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. The two factory methods that are not passed a Locale argument produce a DateFormat object that will format a date string according to the rules of the default locale. The signatures that accepts a style int also format it according to the given style, i.e. SHORT for "M/d/yy" in the US locale.

Globalyzer will detect this method and report it as an I18n issue regardless of the signature used and regardless of whether it is being used correctly. If Locale is already being passed as an argument, Globalyzer will detect it to force developers to double check that the correct Locale is being passed. If you have determined that the call is being handled correctly, you can use Globalyzer's Ignore Comment functionality to ensure that it isn't picked up in a subsequent scan.

I18n Issues

The default machine locale is not always the correct locale for every user. Hence, it is recommended I18n practice to call the factory method that takes a both a style and locale argument, allowing the code to format a date independently of the machine's default locale.

Suggested Replacement

public static final DateFormat getDateInstance(int style,
   Locale aLocale)

Instead of:

DateFormat dateFormatter =
DateFormat.getDateInstance(DateFormat.DEFAULT);

OR

DateFormat dateFormatter =
  DateFormat.getDateInstance();

Use:

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

Please see Times and Dates for more information.

Locale-Sensitive Java Methods

 

Lingoport internationalization and localization services and software