Java / JavaScript日期:这是真的吗? [英] Java/JavaScript dates: Is this true?

查看:49
本文介绍了Java / JavaScript日期:这是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,可以说用户在与应用程序服务器不同的时区上通过浏览器运行我的Web应用程序。我使用JavaScript的 date.getTime()方法在客户端序列化日期。我通过Json发送生成的毫秒数,然后通过调用 new Date(millisecondsFromJS)在服务器端创建一个Java Date对象。我将其存储在MySql中,进行检索,然后通过调用Java的 date.getTime()再次对其进行序列化,然后通过Json将其再次发送给客户端。

So lets say a user is running my web app from his browser on a different time zone than the application server. I serialize a date on the client-side by using JavaScript's date.getTime() method. I send the resulting milliseconds through Json and then create a Java Date object on the server-side by calling new Date(millisecondsFromJS). I store that on MySql, retrieve it, serialize it again by calling Java's date.getTime() and send it once again to the client through Json.

如果我用这些毫秒创建一个JavaScript Date对象,它将得到原始日期吗?我已经成功完成了此过程,但是客户端和服务器当前处于同一时区。我不确定如果时区不同,日期是否会在过程中损坏。

If I create a JavaScript Date object with those milliseconds, will it result in the original date? I've successfully gone through this process, but the client and server are currently on the same time zone. I'm not sure if the date will get corrupted in the process if the time zones are different.

据我了解,使用getTime()返回即时时间与时区无关。例如,如果用户在CDT的2012年7月17日下午4:39捕获了服务器,则服务器可能会将其存储为CEST的2012年7月17日在11:39 PM,但是一旦服务器将其转换为毫秒,因为GMT和客户端创建了一个如果以这些毫秒为单位,那么它可以成功地重建原始的CDT 2012年7月17日下午4:39。

As I understand it, using getTime() returns an instant in time that is independent of time zones. If the user captured July 17, 2012 at 4:39 PM CDT, the server might store it as July 17, 2012 at 11:39 PM CEST, for example, but once the server converts this to milliseconds since GMT and the client creates a date from these milliseconds, it would have successfully reconstructed the original July 17, 2012 at 4:39 PM CDT. Is this true?

推荐答案

在文章在Dropbox上学习的扩展课程,第1部分


内部保留UTC中的所有内容!服务器时间,数据库中的内容等。这将节省很多麻烦,而不仅仅是夏时制。某些软件甚至无法正确处理非UTC时间,因此不要这样做!我们将墙上的时钟设置为UTC。当您想向用户显示时间时,请在最后一秒进行时区转换。

Keep everything in UTC internally! Server times, stuff in the database, etc. This will save lots of headaches, and not just daylight savings time. Some software just doesn’t even handle non-UTC time properly, so don’t do it! We kept a clock on the wall set to UTC. When you want to display times to a user, make the timezone conversion happen at the last second.






将unix时间毫秒发送到服务器,您知道哪个时间点用户选择。然后使用UTC处理服务器上的所有内容,并将毫秒整数返回给客户端。


Send the unix time milliseconds to the server and you know which point of time the user selected. Then handle everything on your server in UTC and return the millisecond integer back to the client.

客户端/ JavaScript:

Client / JavaScript:

var date = new Date();
var clientMilliseconds = date.getTime();
// send clientMilliseconds to server

服务器/ Java:

Date date = new Date(clientMilliseconds);
// store the date, then get it back
long serverMilliseconds = date.getTime();
// send serverMilliseconds back to client

客户端/ JavaScript:

Client / JavaScript:

var date = new Date(serverMilliseconds);
// If receiving the error "Invalid Date", serverMilliseconds
// needs to be converted to an Integer. Consider:
// parseInt: parseInt(serverMilliseconds, 10)
// unary +:  (+serverMilliseconds)

一路走来,服务器和客户端上的 date 对象都会反映各自的时区,因此,如果您同时查看这两个时区,似乎有所不同,但是如果您使用相同的时区将它们转换回UTC,则不是。

Along the way, the date objects on the server and on the client will both reflect the respective timezones so if you look at both, they might seem different, but they aren't if you convert them back to UTC using the same timezone.

所以要回答您的问题问题:

So to answer your question:


如果我用这些毫秒创建一个JavaScript Date对象,它将导致原始日期吗?

If I create a JavaScript Date object with those milliseconds, will it result in the original date?

是。

Java的 Date(长日期)构造函数 getTime()方法的运行时间为unix时间毫秒。 JavaScript的 getTime() Date 构造函数。除了世界标准时间(GMT / UTC)。

Java's Date(long date) constructor and getTime() method operate with unix time milliseconds. So does JavaScript's getTime() and Date constructor. There should be no other timezone involved than Coordinated Universal Time (GMT/UTC).

这篇关于Java / JavaScript日期:这是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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