为什么时间转换为长时会做奇怪的事情? Java

Why is Time doing strange things when converted to long? Java

Time t = Time.valueOf("00:00:00");
long l = t.getTime(); // why is l -3600000

我想将我的时间值转换为秒,以便进行加减运算。

我认为你是 运行 英国的这个代码。或者至少,在默认时区为 Europe/London 的 JVM 上。或者至少比 Unix 纪元的 UTC 早 1 小时的时区。

Here's an Ideone demo.

    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Time t = Time.valueOf("00:00:00");
    long l = t.getTime(); // why is l -3600000
    System.out.println(l);

原因是伦敦在Unix纪元是永久夏令时,所以伦敦纪元的实际时间是1970/1/101:00:00.

因此,该时区的 1970/1/1 00:00:00 比纪元早 60 分钟(或 3600000 毫秒)。

Here's a fork of the same demo, setting the timezone to UTC。这将打印零。