简体   繁体   English

客户端和服务器端(GWT)之间的日期更改

[英]dates change between client and server side (GWT)

I have a problem that I can't understand. 我有一个我无法理解的问题。 I tried to find the answer a lot of time, but without success. 我试图找到答案的时间很多,但都没有成功。

I work with GWT client side, and Java server side. 我在GWT客户端和Java服务器端工作。

Client side, I read dates (java.util.date). 客户端,我读取日期(java.util.date)。 And when I send these dates to server side, there sometimes is an hour offset when I receive it. 当我将这些日期发送到服务器端时,有时会收到一个小时的偏移量。 I know there are many problems with TimeZone. 我知道TimeZone存在很多问题。 But I think TimeZone aren't responsible of my problem, because not all dates are wrong. 但我认为TimeZone对我的问题不负责任,因为并非所有日期都不正确。 To test which dates were wrong, I create a method which create a List of all dates between 1st January of 1900 and today, and which send this list to the server. 为了测试哪些日期是错误的,我创建了一个方法,该方法创建一个1900年1月1日到今天之间所有日期的列表,然后将其发送到服务器。

When I read the list received in server, here are the results : 当我阅读服务器中收到的列表时,结果如下:

  • All dates are correct from the year 1995 (dates didn't change during sending) 所有日期均为1995年以来的正确日期(发送过程中日期未更改)
  • From 1979 to 1995 (approximately): All dates are correct, except 28 days in september / october (from the swith to winter daylight saving time). 从1979年到1995年(大约):除9月/ 10月的28天(从开始到冬季的夏令时)外,所有日期都是正确的。 It's incorrect because of an offset of one hour. 因为有一个小时的偏移,所以它是不正确的。
  • Before : some dates are correct, and some incorrect. 之前:某些日期正确,而某些日期不正确。

So I tried to add 100 years to my dates client side, send it and remove 100 years server side. 因此,我尝试将100年添加到我的日期客户端,发送并删除100年服务器端。 And all received dates were correct ! 并且所有收到的日期都是正确的!

Anybody already had this problem ? 有人已经有这个问题吗? And anybody understood this problem ? 有人知道这个问题吗? Any help is welcome. 欢迎任何帮助。

Thanks ! 谢谢 !

Edit : 编辑:

Ok I solved the problem. 好的,我解决了问题。 Read answer of Andrei Volgin to understand the problem. 阅读Andrei Volgin的答案以了解问题所在。 And here is the code which solved it : 这是解决它的代码:

// Create date you want
Date date = new Date()

// Get TimeZone of your date
TimeZone timeZone = TimeZone.createTimeZone(date.getTimezoneOffset());

// Adapt your date with the TimeZone
date.setTime(date.getTime() - (timeZone.getOffset(date) * 60000));

// You can send your date to server
// TimeZone server side is "UTC", and all dates received are correct

This is a TimeZone problem. 这是一个时区问题。

TimeZone definitions and especially daylight savings rules have changed over the years. 多年来,TimeZone的定义,尤其是夏令时规则已经发生了变化。 If you simply pass the time zone ID or create a time zone using an offset, the browser is unaware of these changes. 如果您只是传递时区ID或使用偏移量创建时区,则浏览器将不会意识到这些更改。 So the browser simply uses the time-zone offset and current DST setting for this time zone when displaying time. 因此,浏览器在显示时间时仅使用该时区的时区偏移量和当前DST设置。 Most of the time this results in a correct time, but not always. 在大多数情况下,这会导致正确的时间,但并非总是如此。 This also explains why all dates in the future are correct. 这也解释了为什么将来的所有日期都是正确的。

In order to get the accurate conversion, you need to create a TimeZone object using a JSON data string that GWT provides, ie use createTimeZone(java.lang.String tzJSON) or createTimeZone(TimeZoneInfo timezoneData) . 为了获得准确的转换,您需要使用GWT提供的JSON数据字符串创建TimeZone对象 ,即使用createTimeZone(java.lang.String tzJSON)createTimeZone(TimeZoneInfo timezoneData)

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

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