gpt4 book ai didi

java - 使用 GWT 创建 ISO 8601 日期字符串

转载 作者:行者123 更新时间:2023-12-02 11:57:22 26 4
gpt4 key购买 nike

这是我的代码:

public static String getStringFormat(Date inputDate, String timeZone){
String strFormat = null;
try{
final TimeZone computedTimeZone = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(timeZone));
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.ISO_8601);
strFormat = dateTimeFormat.format(inputDate, computedTimeZone);
Date d = new Date(strFormat);
strFormat = dateTimeFormat.format(d, TimeZone.createTimeZone(0));
String[] s = strFormat.split("\\+");
strFormat = s[0];
}catch(Exception e){
Console.log(e.getMessage());
}
return strFormat;
}

对于输入,new Date()Etc/GMT+3 此函数返回 null。可能出了什么问题?

错误

Error: NullPointerException: undefined
at NBF_g$.QBF_g$ [as createError_0_g$] (NullPointerException.java:40)
at NBF_g$.ub_g$ [as initializeBackingError_0_g$] (Throwable.java:113)
at NBF_g$.bb_g$ (Throwable.java:61)
at NBF_g$.Ib_g$ (Exception.java:25)
at NBF_g$.avq_g$ (RuntimeException.java:25)
at NBF_g$.gfs_g$ (JsException.java:34)
at new NBF_g$ (NullPointerException.java:27)
at new wou_g$ (JSONString.java:43)

最佳答案

方法 TimeZoneInfo.buildTimeZoneData(String tzJSON) 不接受区域名称,但需要一个包含该区域工作方式详细信息的 JSON 字符串。事实证明,浏览器不会向您提供所有时区如何工作的所有详细信息,因此您的应用程序必须准备好处理它们。

GWT 附带了所有时区(尽管它们目前有点过时,应该在下一个版本中更新),但您必须告诉编译器您想要哪些时区,否则它会将它们编译出来。所有可能的时区及其偏移量等的完整列表并不小,因此我鼓励您限制该列表。

这些存储在常量接口(interface)TimeZoneConstants中。以下是您可以如何使用它:

TimeZoneConstants constants = GWT.create(TimeZoneConstants.class);

// This is the shorthand for TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(...))
TimeZone computedTimeZone = TimeZone.createTimeZone(constants.americaAdak());
//...

如果您想使用时区字符串(例如从服务器传递的时区字符串),您可以构建可能受支持的时区的映射。请注意,完整 map 非常大(仅“美国/...”组中的时区就有 200KB)。

computedTimeZone = TimeZone.createTimeZone(constants.americaAdak());
zones.put(computedTimeZone.getID(), computedTimeZone);
computedTimeZone = TimeZone.createTimeZone(constants.americaAnchorage());
zones.put(computedTimeZone.getID(), computedTimeZone);
computedTimeZone = TimeZone.createTimeZone(constants.americaAnguilla());
zones.put(computedTimeZone.getID(), computedTimeZone);
//...

然后您可以根据需要从 map 上读出特定项目:

String tzName = Window.prompt("Enter a timezone name", "America/Chicago");

DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.ISO_8601);
String strFormat = dateTimeFormat.format(inputDate, zones.get(tzName));
//...
<小时/>

在您的评论中,您澄清了这个问题,即您只需要处理偏移量,而不是完整的时区字符串格式,即 Etc/GMT+3,意思是“从格林威治标准时间”。这更容易处理 - 只需将 +3 解析为数字,然后使用 TimeZone.createTimeZone(int timeZoneOffsetInMinutes) 方法即可。这将无法理解夏令时,但如果没有时区的全名或偏移列表等,这是不可能的(这就是 JSON 如此大的原因)。

//TODO, implement parse(), don't forget about negative numbers
int offsetInHours = parse(timeZone);
TimeZone computedTimeZone = TimeZone.createTimeZone(60 * offsetInHours);
//...

关于java - 使用 GWT 创建 ISO 8601 日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47511079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com