时间戳偏差Java vs Javascript for旧日期(3600秒) [英] Timestamp deviation Java vs Javascript for old dates (3600secs)

查看:260
本文介绍了时间戳偏差Java vs Javascript for旧日期(3600秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将字符串日期表达式转换为数值时,我将在Java / Groovy / PHP vs中获得不同的结果。在1970年之前的某些日期,JS时间戳是Java时间戳之前正好3600秒。我可以在10月1日重现它,但是对于1月1号,没关系。

When converting string date representation to numeric values, I obtain a different result in Java/Groovy/PHP vs in Javascript. For some dates before 1970, the JS timestamp is exactly 3600 secs before the Java timestamp. I could reproduce it for Oct 1st, but for Jan 1st it's ok.

我的测试用例(以groovy,使用通常的Java API为目的):

My test case (in groovy, using the usual Java API on purpose):

def sdf = new SimpleDateFormat("dd/MM/yyyy")
["01/10/1956", "01/01/1956", "01/10/1978"].each {
    def d = sdf.parse(it)
    println "${it} -> ${d.time}"
}

和JS(我只是从Chrome控制台运行它 - 9是10月在这里):

and in JS (I simply run it from the Chrome console - "9" is October here):

new Date(1956, 9, 1, 0, 0, 0).getTime()

几个示例:

* Groovy


  1. 01/10/1956 - > -418179600000

  2. 01/01/1956 - > -441853200000

  3. 01/10/1978 - > 276040800000

* Javascript

*Javascript


  1. 1956,9,1,0,0,0 - > -418183200000

  2. 1956,0,1,0,0, 0 - > -441853200000

  3. 1978,9,1,0,0,0 - > 276040800000

=>注意01/10/1956如何不传播相同的方式,产生3600秒的差异。

=> Notice how 01/10/1956 is not converted the same way, yielding a 3600 seconds difference.

夏令时或时区将是完美的罪魁祸首,但我不明白为什么两个宇宙分歧

A daylight saving time or a timezone would be the perfect culprit but I don't see why the two universe diverged at some point in the past.

任何提示欢迎!

谢谢

编辑更多样本

* Java / Groovy

*Java/Groovy

01/01/1974 -> 126226800000
01/10/1974 -> 149814000000
01/01/1976 -> 189298800000
01/10/1976 -> 212972400000
01/01/1978 -> 252457200000
01/10/1978 -> 276040800000    

* JS

new Date(1974, 0, 1, 0, 0, 0).getTime()    126226800000
new Date(1974, 9, 1, 0, 0, 0).getTime()    149814000000
new Date(1976, 0, 1, 0, 0, 0).getTime()    189298800000
new Date(1976, 9, 1, 0, 0, 0).getTime()    212972400000
new Date(1978, 0, 1, 0, 0, 0).getTime()    252457200000
new Date(1978, 9, 1, 0, 0, 0).getTime()    276040800000

1967〜1971年左右

Around 1967~1971

01/01/1967 -> -94698000000
01/04/1967 -> -86922000000
01/10/1967 -> -71110800000    
01/01/1968 -> -63162000000
01/04/1968 -> -55299600000
01/10/1968 -> -39488400000
01/01/1971 -> 31532400000
01/10/1971 -> 55119600000

new Date(1967, 0, 1, 0, 0, 0).getTime()   -94698000000
new Date(1967, 3, 1, 0, 0, 0).getTime()   -86925600000
new Date(1967, 9, 1, 0, 0, 0).getTime()   -71114400000
new Date(1968, 0, 1, 0, 0, 0).getTime()   -63162000000
new Date(1968, 3, 1, 0, 0, 0).getTime()   -55303200000
new Date(1968, 9, 1, 0, 0, 0).getTime()   -39492000000
new Date(1971, 0, 1, 0, 0, 0).getTime()   31532400000   
new Date(1971, 9, 1, 0, 0, 0).getTime()   55119600000   


推荐答案

您的个人资料说你来自比利时。

Your profile says you're from Belgium.

在1976年布鲁塞尔没有夏令时:

There's no daylight saving time in 1976 for Brussels:

http://www.timeanddate.com/worldclock/clockchange.html?n=48&year=1976

但是从1977年起:

http://www.timeanddate.com/worldclock/clockchange.html?n=48&year=1977

Java可能意识到这一点,而JavaScript不是。

Java is probably aware of this, whereas JavaScript is not.

这篇关于时间戳偏差Java vs Javascript for旧日期(3600秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆