Internationalization and localization tools

Locale-Sensitive Java Method


public SimpleDateFormat()

public SimpleDateFormat(String pattern)

public SimpleDateFormat(String pattern,
   Locale locale)

public SimpleDateFormat(String pattern,
   DateFormatSymbols formatSymbols)

Internationalization (I18n) Method Overview

SimpleDateFormat is a concrete class for formatting and parsing dates. It allows for formatting (date -> text), parsing (text -> date), and normalization.

SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter that can be initialized with a locale-sensitive pattern.

I18n Issues

While the SimpleDateFormat class provides convenient mechanisms for turning a Date string into a Date object and vice versa, its reliance on user-entered patterns prevents it from generating a date string that is formatted according to the user's locale. If the date string being generated is intended for display to the user, the DateFormat class should be used in its place.

Globalyzer will detect this method regardless of whether the date string being generated is intended for end-user display or whether it is purely programmatic and hence not locale-sensitive. If you have determined that the call does not pose I18n problems, 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 getDateInstance(int style,
   Locale aLocale)

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

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

Instead of:

SimpleDateFormat formatter =
   new SimpleDateFormat("MM/dd/yy");
String dateString = formatter.format(new Date());


DateFormat df = DateFormat.getDateInstance(
   DateFormat.SHORT, getUserLocale());
String dateString = formatter.format(new Date());

Locale-Sensitive Java Methods


Lingoport internationalization and localization services and software