PRB: Date/Time Handling and Locale-Specific Issues (293952)



The information in this article applies to:

  • Microsoft virtual machine
  • Microsoft Visual J++ 6.0

This article was previously published under Q293952

SYMPTOMS

When you use methods of the SimpleDateFormat class, the results depend on the default locale that is set in your system. The following sample code illustrates this behavior:
SimpleDateFormat sdf = new SimpleDateFormat();	
String pat = sdf.toPattern();
int success = pat.compareTo( "M/d/yy h:mm a" );
				
In this case, the comparison result may be negative on a system in which the English/US locale is not set as the default.

The methods of the GregorianCalendar class are also locale-dependent, as shown in the following code sample:
GregorianCalendar testcalender = new GregorianCalendar();
int hashcode = testcalender.hashCode();
boolean success = (hashcode == 0)
				
Also, the hash code of GregorianCalendar appears as follows:
public synchronized int hashCode()
{
   return getFirstDayOfWeek() ^ getMinimalDaysInFirstWeek();
}
				

CAUSE

This problem occurs because the toPattern method of SimpleDateFormat returns a pattern string that describes the date format in locale-specific mode. For example, when you set the locale to Japanese and call toPattern, this method returns "MM/dd HH:mm".

In the GregorianCalendar class, the values of the FirstDayOfWeek and MinimalDaysInFirstWeek properties are also locale-specific. These values come from the locale data in the java.text.resources package. For example, on German systems, the values are 2 and 4 respectively, which causes the hash code to return a non-zero value.

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbFAQ kbJavaFAQ kbprb KB293952