Internationalization and localization tools


Locale-Sensitive Java Method

java.util.DateFormat

public static final DateFormat getDateTimeInstance()

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

public static final DateFormat getDateTimeInstance(
   int dateStyle,
   int timeStyle,
   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 don't take a Locale argument produce a date/time formatter that will format a date/time string according to the rules of the default locale. The signatures that accept the two style int arguments also format it according to the given style, i.e.

  • dateStyle = SHORT for "M/d/yy" in the US locale
  • timeStyle = SHORT for "h:mm a" in the US locale

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 the two style arguments as well as a locale argument, allowing the code to format the date/time string independently of the machine's default 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.

Suggested Replacement

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

Instead of:

DateFormat dateFormatter =
  DateFormat.getDateTimeInstance(
  DateFormat.DEFAULT, DateFormat.DEFAULT);

OR

DateFormat dateFormatter =
  DateFormat.getDateTimeInstance();

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