Microsoft KB Archive/293952

= PRB: Date/Time Handling and Locale-Specific Issues =

Article ID: 293952

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Visual J++ 6.0 Standard Edition

-



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( &quot;M/d/yy h:mm a&quot; ); 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 &quot;MM/dd HH:mm&quot;.

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.

