将日期和时区从 GAE 服务器发送到 GWT 客户端 [英] Sending a date and timezone from GAE server to GWT client

查看:14
本文介绍了将日期和时区从 GAE 服务器发送到 GWT 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的伙计们,我已经解决这个问题大约 2 周了,现在我尝试了所有我能想到的方法,并查看了这里的大量答案,感觉他们会回答这个问题,但我无法弄清楚我是如何解决这个问题的我打算做我想做的事情,这让我绝对疯了!似乎没有人在任何地方有答案,到处都是一半的答案,但似乎没有人真正做我需要做的事情,肯定有人以前解决过这个问题!请原谅这个非常长的问题,但重要的是要正确地提出问题,这样答案才能真正满足需求!:D

OK folks, I have been going around this problem for about 2 weeks now trying everything I can think of and looking at tons of answers on here that feel like they are going to answer it, but I just cant work out how I am meant to do what I am trying to do and it is driving me absolutely NUTS! No one seems to have an answer anywhere, there are half answers all over the place, but none seem to actually do what I need it to do, surely SOMEONE has solved this problem before! Please excuse the REALLY long question, but its important that the question is asked right, so that the answers actually answer the need! :D

另外,如果您可能提供的答案没有考虑到我的数据存储/服务器是 Google App Engine (GAE),并且我的客户端是用 Google Web Toolkit (GWT) 编写的,请不要再费心阅读) 并且所有内容都是用 Java 编写的,我对 Python、MySQL 或 PHP 或类似的东西一无所知

Also please don't bother reading any further if you are likely to provide any answer which does not take into account that my datastore/server is Google App Engine (GAE), and my Client is written in Google Web Toolkit (GWT) and everything is written in Java, I dont know anything about Python or MySQL or PHP or anything like that

最后,如果有人知道一个更好的地方来解决这样的问题,请告诉我,因为我一直在提出这样的问题并且不知道除了在这里之外如何找到解决方案,或者花几个小时和数小时在谷歌上搜索已经设法弄清楚的其他人.我也不明白如何从 JavaDocs 中找出任何东西 - 抱歉,但在有人向我解释之前,我只是不明白

Finally if anyone knows of a better place to figure stuff out like this, PLEASE do let me know as I keep coming up with issues like this and have no idea how to find a solution other than in here, or spend hours and hours searching google for someone else who has already managed to figure it out. I also dont understand how to figure anything out from JavaDocs - sorry, but until someone explains it to me, Im just not going to get it

我在服务器上存储了一个日期时间(显然是 UTC)我还在服务器上为每个企业主存储了一个时区,以字符串形式存储(例如 "America/New_York").该字符串是通过在服务器上使用 java.util.TimeZone.getAvailableIDs() 并让企业主为其企业选择相关时区而获得的(默认为 "Europe/London" 如果他们没有选择任何东西 - 不需要挂断那一点)我需要在未来的某个时候将任何日期时间传递回客户端 PC,忽略他们 PC 上的本地时区,因为日期时间需要根据企业主所在的时区显示.

I have a datetime stored on the server (in UTC obviously) I also have stored on the server a TimeZone for each business owner stored as a string (eg "America/New_York"). This string is obtained by using java.util.TimeZone.getAvailableIDs() on the server and getting the business owner to select the relavant timezone for their business (default is "Europe/London" if they don't choose anything - no need to get hung up on that bit) I need to pass any datetimes back to the client PC at some point in the future, IGNORING the local timezone on their PC, because the datetime needs to be displayed according to the timezone of the business owner.

举个例子,企业主是伦敦的一位美发师,他们有一个在线预约时间表,可供他们的任何潜在客户查看.其中一位客户(也在伦敦)想要预约,在英国夏令时 9 月 3 日星期一上午 9 点看到一个空档并进行预订.

So as an example the business owner is a hairdressers in London and they have an online appointment schedule which is available to be viewed by any potential customer of theirs. One of these customers (also in London) wants to book an appointment, sees a slot at 9am BST on Monday 3rd September and books it.

然后将其作为 UTC 时间存储在数据存储区中(实际上是 0800,因为 BST 是 UTC+1),当然企业主选择了 Europe/London" 作为他们的时区.到目前为止一切正常.

This is then stored in the Datastore as the UTC time (which is actually 0800 as BST is UTC+1), and of course the business owner has selected "Europe/London" as their timezone. Everything fine so far.

现在,在未来的某个时间点,客户正在纽约参加商务会议,并意识到他们有一个下周在伦敦理发的预约,并且他们想将它提前一周,所以他们然后尝试查看约会(在理发师预约簿中),但他们不想将其视为纽约时间凌晨 4 点,他们希望将其视为英国时间上午 9 点.

NOW, at some point in the future, the customer is on a business meeting in New York, and realises that they have an appointment the following week for a haircut in London and they want to move it by a week, so they then try to VIEW the appointment (in the hairdressers appointment book), but they don't want to see it as 4am New York Time, they want to see it as 9am British time.

所以我的问题是,当客户在 9 月 3 日点击查看约会时,服务器可以发回 UTC 日期时间 0800 和 java.util.TimeZone ID 字符串 "欧洲/伦敦"

So my problem is that when the customer clicks to VIEW appointments on 3rd September, the server can send back a UTC datetime of 0800 and a java.util.TimeZone ID String of "Europe/London"

但是因为在客户端我只能使用

But because on the client I can only use

com.google.gwt.i18n.client.TimeZone

它不允许我使用该功能

TimeZone bizTimeZone=TimeZone.getTimeZone("Europe/London")
(which works for `java.util.TimeZone`)

因为它导致错误

"The method getTimeZone(String) is undefined for the type TimeZone"

它建议将其更改为

com.google.gwt.i18n.client.TimeZone.createTimeZone("Europe/London")

但此版本需要 GWT com.google.gwt.i18n.client.TimeZoneConstants对象"(此消息末尾显示的示例),而不是 com.google.gwt.i18n.client.TimeZoneConstantscode>java.util.TimeZone.createTimeZone() 将接受.

but this version, is expecting a GWT com.google.gwt.i18n.client.TimeZoneConstants "object" (example shown at the end of this message) and not the simple string that the java.util.TimeZone.createTimeZone() will accept.

我可以使用以下方法创建它:

I can create this by using:

final TimeZoneConstants constTz = GWT.create(TimeZoneConstants.class);
final TimeZone timeZoneCali = TimeZone.createTimeZone(constTz.europeLondon());

但是当我以 "Europe/London" 开头和/或我不知道如何将此对象存储到 App Engine 数据库中然后稍后检索它时,我不知道如何执行此操作.

But I dont know how to do this when Im starting with "Europe/London" and/or I dont know how to store this object into the App Engine DataBase and then retrieve it later.

顺便说一句,在评论文本中,我也尝试使用以下几行:

Incidentally amongst the commented text I have also tried using the following lines:

//Calendar tmad = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"));
//Calendar cal = Calendar.getInstance(someTimeZone);
//DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//df1.setTimeZone(TimeZone.getTimeZone("Europe/London"));
//final TimeZone timeZoneClient = TimeZone.createTimeZone("Europe/London");
//dtf.format(date,timeZoneClient)

肯定有一种标准的方法可以做到这一点,我真的不能成为第一个必须解决这个问题的人!

There MUST be a standard way of doing this SURELY, I really cant be the first person to ever have to solve this issue!

constTz.europeLondon()={"transitions": [19394, 60, 24770, 0, 28130,60, 33506, 0, 36866, 60, 42242, 0, 45602, 60, 50978, 0, 54506, 60,59714, 0, 63242, 60, 68450, 0, 71978, 60, 77354, 0, 80714, 60, 86090,0, 89450, 60, 94826, 0, 98521, 60, 103561, 0, 107257, 60, 112297, 0,115993, 60, 121033, 0, 124729, 60, 129937, 0, 133633, 60, 138673, 0,142369, 60, 147409, 0, 151105, 60, 156145, 0, 159841, 60, 164881, 0,168577, 60, 173785, 0, 177313, 60, 182521, 0, 186217, 60, 191257, 0,194953, 60, 199993, 0, 203689, 60, 208729, 0, 212425, 60, 217465, 0,221161, 60, 226201, 0, 230065, 60, 235105, 0, 238801, 60, 243841, 0,247537, 60, 252577, 0, 256273, 60, 261481, 0, 265009, 60, 270217, 0,273745, 60, 278953, 0, 282649, 60, 287689, 0, 291385, 60, 296425, 0,300121, 60, 305329, 0, 308857, 60, 314065, 0, 317593, 60, 322801, 0,326329, 60, 331537, 0, 335233, 60, 340273, 0, 343969, 60, 349009, 0,352705, 60, 357913, 0, 361441, 60, 366649, 0, 370177, 60, 375385, 0,379081, 60, 384121, 0, 387817, 60, 392857, 0, 396553, 60, 401593, 0,405289, 60, 410497, 0, 414025, 60, 419233, 0, 422761, 60, 427969, 0,431665, 60, 436705, 0, 440401, 60, 445441, 0, 449137, 60, 454345, 0,457873, 60, 463081, 0, 466609, 60, 471817, 0, 475513, 60, 480553, 0,484249, 60, 489289, 0, 492985, 60, 498025, 0, 501721, 60, 506929, 0,510457, 60, 515665, 0, 519193, 60, 524401, 0, 528097, 60, 533137, 0,536833, 60, 541873, 0, 545569, 60, 550777, 0, 554305, 60, 559513, 0,563041, 60, 568249, 0, 571777, 60, 576985, 0, 580681, 60, 585721, 0,589417, 60, 594457, 0], "names": ["GMT", "Greenwich Mean Time", "BST","英国夏令时"], "id": "欧洲/伦敦", "std_offset": 0}

constTz.europeLondon()={"transitions": [19394, 60, 24770, 0, 28130, 60, 33506, 0, 36866, 60, 42242, 0, 45602, 60, 50978, 0, 54506, 60, 59714, 0, 63242, 60, 68450, 0, 71978, 60, 77354, 0, 80714, 60, 86090, 0, 89450, 60, 94826, 0, 98521, 60, 103561, 0, 107257, 60, 112297, 0, 115993, 60, 121033, 0, 124729, 60, 129937, 0, 133633, 60, 138673, 0, 142369, 60, 147409, 0, 151105, 60, 156145, 0, 159841, 60, 164881, 0, 168577, 60, 173785, 0, 177313, 60, 182521, 0, 186217, 60, 191257, 0, 194953, 60, 199993, 0, 203689, 60, 208729, 0, 212425, 60, 217465, 0, 221161, 60, 226201, 0, 230065, 60, 235105, 0, 238801, 60, 243841, 0, 247537, 60, 252577, 0, 256273, 60, 261481, 0, 265009, 60, 270217, 0, 273745, 60, 278953, 0, 282649, 60, 287689, 0, 291385, 60, 296425, 0, 300121, 60, 305329, 0, 308857, 60, 314065, 0, 317593, 60, 322801, 0, 326329, 60, 331537, 0, 335233, 60, 340273, 0, 343969, 60, 349009, 0, 352705, 60, 357913, 0, 361441, 60, 366649, 0, 370177, 60, 375385, 0, 379081, 60, 384121, 0, 387817, 60, 392857, 0, 396553, 60, 401593, 0, 405289, 60, 410497, 0, 414025, 60, 419233, 0, 422761, 60, 427969, 0, 431665, 60, 436705, 0, 440401, 60, 445441, 0, 449137, 60, 454345, 0, 457873, 60, 463081, 0, 466609, 60, 471817, 0, 475513, 60, 480553, 0, 484249, 60, 489289, 0, 492985, 60, 498025, 0, 501721, 60, 506929, 0, 510457, 60, 515665, 0, 519193, 60, 524401, 0, 528097, 60, 533137, 0, 536833, 60, 541873, 0, 545569, 60, 550777, 0, 554305, 60, 559513, 0, 563041, 60, 568249, 0, 571777, 60, 576985, 0, 580681, 60, 585721, 0, 589417, 60, 594457, 0], "names": ["GMT", "Greenwich Mean Time", "BST", "British Summer Time"], "id": "Europe/London", "std_offset": 0}

推荐答案

我在 v2.4 中遇到了类似的问题.

I have a similar issue with v2.4.

我认为莱利·拉克是对的.

I think Riley Lark is right.

TimeZone.createTimeZone("Asia/Tokyo") 在 GWT 2.4 中不起作用

TimeZone.createTimeZone("Asia/Tokyo") did not work in GWT 2.4

但是,我发现我可以通过以下方式使用浏览器的默认设置获取东京区域:

However, I found I could get the Tokyo zone by using the browser's default setting via:

Date d = new Date();
TimeZone timezone = TimeZone.createTimeZone(d.getTimezoneOffset());

这正是 Riley Lark 给你的建议,因为 europeLondon 有 "std_offset": 0...

That is really what Riley Lark is suggesting for you since europeLondon has "std_offset": 0...

TimeZone 时区 = TimeZone.createTimeZone(0);

TimeZone timezone = TimeZone.createTimeZone(0);

这篇关于将日期和时区从 GAE 服务器发送到 GWT 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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