निम्नलिखित कोड java.util.Date में एक बग प्रदर्शित करने के लिए प्रतीत होता है, जिससे एक घंटा जोड़ा जाता है, यदि स्थानीय घड़ी जीएसटी पर डीएसटी समायोजन के साथ सेट की जाती है और समय 1 नवंबर 1 9 71 से पहले होता है। मेरी पहली धारणा हमेशा होती है कि मैं क्या यह गलत है। क्या कोई देख सकता है कि क्या गलत है (या यह वास्तव में जावा बग है)? 1 नवंबर 1 9 71 के बारे में क्या महत्वपूर्ण है?1 नवंबर 1 9 71 से पहले तारीखों के लिए java.util.Date पर एक घंटे क्यों जोड़ा जाता है?
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
class JavaUtilDateBug
{
private static void demo() throws Exception
{
// UK developers usually have the clock on their development machines set
// to "Europe/London" (i.e. GMT with daylight saving). Set it explicitly
// here so readers in other countries can see the problem too.
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
Locale.setDefault(Locale.ENGLISH);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String strJan1st1970Expected = "Thu Jan 01 00:00:00 GMT 1970";
String strJan1st1970Actual = dateFormat.parse(strJan1st1970Expected).toString();
System.out.println("strJan1st1970Actual: " + strJan1st1970Actual); // -> "Thu Jan 01 01:00:00 GMT 1970"
boolean jvmHasDateBug = !strJan1st1970Expected.equals(strJan1st1970Actual);
System.out.println("jvmHasDateBug: " + jvmHasDateBug); // -> true
// The anomaly only seems to affect times before 1 Nov 1971.
final String strNov1st1971 = "Mon Nov 01 00:00:00 GMT 1971";
assert strNov1st1971.equals(dateFormat.parse(strNov1st1971).toString());
}
public static void main(String[] args)
{
try
{
demo();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
मेरे जावा वातावरण:
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
आपका डेमो प्रोग्राम केवल गैर-अंग्रेज़ी मशीनों पर काम करता है यदि आप लोकेल भी सेट करते हैं (दिनांक पार्सिंग अन्यथा विफल रहता है)। –
धन्यवाद, माइकल। डेमो कोड अपडेट किया गया। –