简体   繁体   English

为什么java.sql.Date.getTime()对于1970-01-02返回82_800_000而不是86_400_000?

[英]Why does java.sql.Date.getTime() returns 82_800_000 for 1970-01-02 instead of 86_400_000?

One day has 86400 seconds. 一天有86400秒。 Many time objects are related to the Epoch on 1970-01-01. 许多时间对象与1970-01-01的纪元有关。 The API says API说

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. 毫秒值左右的精简包装,可让JDBC将其标识为SQL DATE值。 A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT 毫秒值表示自格林威治标准时间1970年1月1日00:00:00.000起经过的毫秒数

The following code returns 82800000. Why? 以下代码返回82800000。为什么? What is wrong? 怎么了?

java.sql.Date sqlDate = java.sql.Date.valueOf( "1970-01-02" );
long millis = sqlDate.getTime();
System.out.println( "expected=" + 86400000 + " " + sqlDate
            + "\n  result=" + millis );

A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT 毫秒值表示自格林威治标准时间1970年1月1日00:00:00.000起经过的毫秒数

So if your time zone is GMT+1, you will get 82800000 for January 2, 1970: 因此,如果您的时区是GMT + 1,则1970年1月2日将为82800000:

(January 1, 1970 + one day - one hour)

Since you're living in Germany, your timezone is CET (or GMT+1 ) 由于您居住在德国,因此时区为CET (或GMT+1

You're parsing the date corresponding to your own timezone so there'll be a difference of one hour between the value you're parsing and the initial beginning. 您正在解析与您自己的时区相对应的日期,因此解析的值与初始时间之间会相差一小时。

That said, the instant is the same. 也就是说,瞬间是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM