以下代码似乎演示了java.util.Date中的一个错误,如果本地时钟设置为GMT且DST调整开启且时间早于1971年11月1日,则会添加一小时.我的第一个假设始终是我的弄错了.任何人都可以看到什么是错的(或者这真的是一个Java错误)?1971年11月1日有什么重要意义?
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的Java环境:
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)
Run Code Online (Sandbox Code Playgroud)
Nic*_*olt 21
1968年10月27日至1971年10月31日之间有英国标准时间的试验,我怀疑这是导致这个问题的原因.
这里有一些试验细节:
http://en.wikipedia.org/wiki/British_Summer_Time#Single.2FDouble_Summer_Time
1月1日欧洲/伦敦时区1970年为英国标准时间(GMT + 1),所以当你使用java.text.SimpleDateFormat解析1月1日00:00:00 GMT 1970年产生正确的时代价值等于为Jan 01 01:00:00 1970年在BST.
然后,由于它的蹩脚java.util.Date,当你打电话时java.util.Date.toString()它使用当前本地的默认时区,现在已经改为GMT,你得到1970年1月1日01:00:00 GMT.
laa*_*lto 13
这是当地的.来自http://en.wikipedia.org/wiki/British_Summer_Time
英国标准时间计划于1968年10月27日至1971年10月31日期间进行了试验,当时英国全年保持GMT + 1.