- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我们存储我们的日期以毫秒为单位存储,因为我们想要显示时间相关数据的对象的纪元和奥尔森时区 ID。
如何将 Olson TZID 转换为 TimeZoneConstant 以创建时区并使用 DateTimeFormat?
// values from database
String tzid = "America/Vancouver";
long date = 1310771967000L;
final TimeZoneConstants tzc = GWT.create(TimeZoneConstants.class);
String tzInfoJSON = MAGIC_FUNCTION(tzid, tzc);
TimeZone tz = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzInfoJSON));
String toDisplay = DateTimeFormat.getFormat("y/M/d h:m:s a v").format(new Date(date), tz);
那个 MAGIC_FUNCTION 存在吗?或者还有其他方法吗?
最佳答案
根据 GWT Javadoc [1],在 TimeZoneConstants 类上执行 GWT.create 是糟糕的游戏。所以我所做的是在服务器端创建一个类来解析/com/google/gwt/i18n/client/constants/TimeZoneConstants.properties 并为每个时区构建所有 JSON 对象的缓存(由他们的 Olson TZID 索引) ).
我的站点在 jboss 上运行,所以我将 TimeZoneConstants.properties 复制到我站点的 war/WEB-INF/lib 目录中(可能不需要将其复制到那里,因为 GWT jar 已经存在)。然后我有一个单例类,它在构造时进行解析:
InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILE);
InputStreamReader isr = new InputStreamReader(inStream);
BufferedReader br = new BufferedReader(isr);
for (String s; (s = br.readLine()) != null;) {
// using a regex to grab the id to use as a key to the hashmap
// a full json parser here would be overkill
Pattern pattern = Pattern.compile("^[A-Za-z]+ = (.*\"id\": \"([A-Za-z_/]+)\".*)$");
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
String id = matcher.group(2);
String json = matcher.group(1);
if (!jsonMap.containsKey(id)) {
jsonMap.put(id, json);
}
}
}
br.close();
isr.close();
inStream.close();
最后,我进行 RPC 调用以将 TimeZoneInfoJSON 发送给客户端(假设服务器知道我对哪个 TimeZoneID 感兴趣):
getTimeZone(new PortalAsyncCallback<String>() {
public void onSuccess(String tzJson) {
timeZone = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(tzJson));
}
});
这不是最优雅的解决方案,但它为我提供了一种通过 DST 转换显示特定时区的日期和时间的方法。
关于java - 将 Olson 时区 ID 转换为 GWT 中的 TimeZoneConstant(客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8528173/
我们想在我们的网站上将 windows 时区转换为 OLSON 时区。然而,OLSON 时区的问题在于它们使用大洲而不是国家名称来命名,例如: America/New_York Asia/Kolkat
TimeZoneInfo 和 Olson 数据库是否使用相同的时区标识符? 我从 GeoNames 服务(基于 Olson 数据库)获取时区 ID,并希望检索该时区的夏令时信息。 最佳答案 unico
如何获得 Olson timezone name (如Australia/Sydney)对应C的localtime给出的值打电话? 这是通过 TZ、符号链接(symbolic link) /etc/l
在Google's example for createEventSeries (如下所示),日期值在末尾指定为“EST”。但是,我想为 recurring event (see type #4 of
我有 1974-2013 年的时间序列数据,其中有一列用于 datetimeUTC (YYYY-MM-DD hh:mm +0000),一列用于奥尔森格式的时区(例如加拿大/太平洋、加拿大/东部) .我
嗯, 时候到了,我正在努力摆脱 Larry Wall 的噩梦,从 Perl 转向 Python(为了工作)。为此,我正在通过密集的自学,从一个半夜的牛仔编码员、一名士兵、非文档 worker ,我相信
我使用向服务器发送一些元数据的旧设备。我需要操纵 UTC 格式的日期时间。我还获取设备位于 POSIX 格式的时区信息,例如。柏林: CET-1CEST,M3.5.0/2,M10.5.0/3 但是,像
我们使用“America/New_York”约定供用户选择时区,但在我们的资源受限系统中,我们必须手动提供实际的 TZ 变量。例如,对于纽约,我们有: timezones["America/New_Y
我有兴趣检索用户当前在 JavaScript 中为其操作系统设置的当前区域信息字符串(“美国/洛杉矶”、“欧洲/伦敦”等)。我已经找到了如何以秒为单位获取当前偏移量、特定日期的偏移量和不太精确的时区(
我有一个遗留应用程序,需要用它来补充一些数据。目前,我们有一个存储美国(及其领土)邮政编码的数据库表,以及一个 GMT 偏移量,以及一个显示该邮政编码是否使用夏令时的标志。这是从一些免费提供商那里下载
我正在寻找一个表来将所有 Olson TZID 转换为 Windows 时区。我发现: .NET TimeZoneInfo from Olson time zone 这很棒,可以根据以下信息构建: h
根据 timezone-js 的自述文件: http://github.com/mde/timezone-js 您需要下载 Olson 时区数据库并将文件存储在您的 Web 服务器上的目录中,以便 t
假设我有给定 Olson/tz 数据库时区的时间,例如,1973 年 7 月 1 日 15 点 23 分钟,非洲/马普托。 如何在 node.js 中将其转换为世界时间 UT,反之亦然?我在网上看到过
我需要将 IANA/Olson 时区 ID 映射到缩写,如 EST、PST 等。 我知道这不是一对一的映射,例如,对于 EST,有很多 IANA 时区。 我可以使用某种数据库/映射吗? PS:Java
我在一个大量使用时区的网站上工作,该网站需要处于可用的绝对最新 tz 信息的最前沿(我已经通过 tz mailing list 获得)。我不知道的是如何让 PHP 安装在我的开发机器和我的 Web 主
我们存储我们的日期以毫秒为单位存储,因为我们想要显示时间相关数据的对象的纪元和奥尔森时区 ID。 如何将 Olson TZID 转换为 TimeZoneConstant 以创建时区并使用 DateTi
我是一名优秀的程序员,十分优秀!