Java GWT 中的客户端时区支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1686448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Client side time zone support in GWT
提问by Hedenius Access
I'm working on a GWT app where I need to support the following scenario:
The server is located in time zone A
The client's browser is set to time zone B
The GWT app is configured to display date/time in time zone C
我正在开发一个需要支持以下场景的 GWT 应用程序:
服务器位于时区 A
客户端的浏览器设置为时区 B
GWT 应用程序配置为在时区 C 中显示日期/时间
Since GWT does not support Calendar and the native support for time zones in javascript is non-existent I can't think of a nice and clean solution to this problem.
由于 GWT 不支持 Calendar 并且在 javascript 中不存在对时区的本机支持,因此我想不出一个好的和干净的解决方案来解决这个问题。
Have any of you done something similar or do you know of any good utils I could use?
你们中有没有人做过类似的事情,或者你知道我可以使用的任何好工具吗?
Thanks!
谢谢!
采纳答案by Upgradingdave
In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt:
根据我的经验,以下最佳实践在处理 gwt 中的日期和时区时显着降低了复杂性和混乱:
- Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference.
- Whenever displaying the date to the end user, format the date using appropriate timezone.
- 每当在应用程序中操作/存储日期时,将所有日期视为自 GMT 时区纪元以来的毫秒数。您可以将它们存储为字符串或整数,这并没有什么区别。
- 每当向最终用户显示日期时,请使用适当的时区格式化日期。
For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat(or write your own date formatter util) to convert it into either timezone B or timezone C as appropriate.
对于您的情况,当您在服务器(时区 A)上创建日期时,将其转换为自 GMT 纪元以来的毫秒数,然后再将其发送到客户端。在客户端上,根据需要使用DateTimeFormat(或编写自己的日期格式化程序实用程序)将其转换为时区 B 或时区 C。
回答by Iker Jimenez
I'm assuming you are using RPC calls for server-client communication here. Also assuming that you don't care about timezone B, and you know what timezone C is on the server.
我假设您在这里使用 RPC 调用进行服务器-客户端通信。还假设您不关心时区 B,并且您知道服务器上的时区 C。
You have a few options here:
您在这里有几个选择:
- Calculate the desired date in the server (no Java limits on what you can do there) and send it in a String to be displayed to the client, so you don't have to do anymore transformations on the client.
- 在服务器中计算所需的日期(Java 对您在那里可以做什么没有限制)并将其发送到要显示给客户端的字符串中,这样您就不必再在客户端上进行转换了。
or:
或者:
- Calculate the offset between timezone A and C on the server, apply it to all the Date objects you are passing to the client and just display them on the client.
- 计算服务器上时区 A 和 C 之间的偏移量,将其应用于您传递给客户端的所有 Date 对象,并在客户端上显示它们。
if for some reason none of these were valid for you
如果由于某种原因这些都不适合你
- Calculate the offset, send it to the client and apply it to any Date you receive from the server by transforming to ms, adding the offset and then creating a Date object again.
- 计算偏移量,将其发送到客户端并将其应用于您从服务器接收到的任何日期,方法是转换为毫秒,添加偏移量,然后再次创建日期对象。
回答by Fedearne
You can't change the GWT timezone, hence all java.util.Date's has the browser timezone. You will need to handle the current timezone setting manually.
您无法更改 GWT 时区,因此所有 java.util.Date 都具有浏览器时区。您需要手动处理当前时区设置。
I see 3 options:
我看到 3 个选项:
You manage the timezone conversion yourself.
You override the serializer/deserializer of java.util.Datelike in this post. And maybe using a custom java.util.Dateimplemtation, that overrides the getTimezoneOffset(). This approach requires recompilation of the GWT API!.
You implement your own Date, either by extending java.util.Date(like in option 2) or wrapping it with some timezone object. In this option CustomFieldSerializer'smay still be usefull, but there is no need for recompiling the GWT API.
您自己管理时区转换。
你像这篇文章一样覆盖了java.util.Date的序列化器/反序列化器。也许使用自定义java.util.Date实现,它会覆盖getTimezoneOffset()。这种方法需要重新编译 GWT API!。
您可以通过扩展java.util.Date(如选项 2)或用某个时区对象包装它来实现自己的 Date 。在此选项中,CustomFieldSerializer可能仍然有用,但不需要重新编译 GWT API。
I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's
我更喜欢选项 3。至少在 GWT RPC 之前也许有一天会支持覆盖 CustomFieldSerializer
回答by Ramesh Ganesan
see this demo project GWT timezone demo project
看到这个演示项目 GWT 时区演示项目
回答by andykellr
Dave Paroulek's answer is the right approach. If you want to see an example of this, we created widgets that work independent of TimeZone and process the values on the server-side where we have all of the TimeZone information we need.
Dave Paroulek 的答案是正确的方法。如果您想查看这样的示例,我们创建了独立于 TimeZone 工作的小部件,并在服务器端处理值,在那里我们拥有我们需要的所有 TimeZone 信息。
UTCDateBox- Wrapper around the GWT DateBox and always chooses the date at midnight in GMT and represents the value as a Long instead of a Date.
UTCDateBox- 包装在 GWT DateBox 周围并始终选择格林威治标准时间午夜的日期,并将值表示为 Long 而不是 Date。
UTCTimeBox- New widget that always chooses a time as millis since midnight, independent of timezone, also represented as a Long.
UTCTimeBox- 新的小部件,它始终选择自午夜起以毫秒为单位的时间,与时区无关,也表示为 Long。
UTCDateTimeUtils- Server-side code that splits a Date into 2 Long values appropriate for UTCDateBox and UTCTimeBox in a given TimeZone and combines them back into a Date in a given TimeZone.
UTCDateTimeUtils- 服务器端代码,将 Date 拆分为 2 个适合给定 TimeZone 中的 UTCDateBox 和 UTCTimeBox 的 Long 值,并将它们组合回给定 TimeZone 中的 Date。
Here is an example of the date the time controls being used together.
Blog article describing their implementation.
回答by Jeff Evans
I created a GWT-compatible Java versionof the jsTimezoneDetect Javascript libraryspecifically for this purpose. This should provide a (very good guess of) the timezone name purely on the client side. Feel free to try it out and let me know if it works or doesn't work for you.
我专门为此目的创建了一个与GWT 兼容的 Java 版本的jsTimezoneDetect Javascript 库。这应该纯粹在客户端提供(非常好的猜测)时区名称。随意尝试一下,让我知道它是否适合您。