Internationalization and localization tools


Locale-Sensitive Java Method

java.util.Calendar

public static Calendar getInstance()

public static Calendar getInstance(TimeZone zone)

public static Calendar getInstance(TimeZone zone,
 Locale aLocale)

Internationalization (I18n) Method Overview

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on. It also provides convenient methods for manipulating the calendar fields, such as getting the date of the next week.

An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian). The zero-argument factory method and a factory method that accepts a TimeZone argument return a Calendar instance that uses the default locale. The zero-argument method assumes the default TimeZone associated with the default locale.

I18n Issues

Because the Calendar of the default locale and time zone might not be correct for all users of the application, it is good I18n practice to call the getInstance method that accepts both a Locale argument and a TimeZone argument specific to that of the current user.

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 both Locale and TimeZone are already being passed as arguments, Globalyzer will detect the call anyway to force developers to double check that the correct values are 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 Calendar getInstance(TimeZone zone,
 Locale aLocale)

Instead of:

Calendar aCalendar =
  Calendar.getInstance();

OR

Calendar aCalendar =
  Calendar.getInstance(
  TimeZone.getTimeZone("PST"));

Use:

//retrieve the run-time user's locale
Locale locale = getUserLocale();

//instantiate a TimeZone object by passing in the user's
//time zone identification string such as "PST"
TimeZone zone = TimeZone.getTimeZone(getUserZone());

//pass the user's locale and time zone as arguments
Calendar aCalendar =
   Calendar.getInstance(zone, locale);

Locale-Sensitive Java Methods

 

Lingoport internationalization and localization services and software