如何使用 Java 的 SimpleDateFormat 解析给出为“GMT+0100 (BST)”的时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7091666/
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
How can I use Java's SimpleDateFormat to parse a timezone given as "GMT+0100 (BST)"?
提问by user898465
I have a date that's in the form of:
我有一个日期形式为:
Wed Aug 17 2011 09:57:09 GMT+0100 (BST)
and have a filter that takes a time in a certain format. The problem seems to be the time zone on the end, none of the format strings I'm putting in the filter seem to work for this type of date format.
并有一个过滤器,它以某种格式需要一段时间。问题似乎是最后的时区,我放入过滤器的格式字符串似乎都不适用于这种类型的日期格式。
For example,
例如,
Wed Aug 17 2011 09:57:09 GMT+0100 (BST)
EEE MMM dd yyyy HH:mm:ss zZ?
The time zone part of this, keeps throwing an error.
其中的时区部分不断抛出错误。
Can anyone tell me what the correct format to parse the time zones on these dates is?
谁能告诉我解析这些日期时区的正确格式是什么?
回答by Konrad Garus
"z" needs a colon between hours and minutes. "Z" is only +/-HHMM (i.e. no "GMT" prefix).
“z”在小时和分钟之间需要一个冒号。“Z”只是 +/-HHMM(即没有“GMT”前缀)。
One way to parse it is: EEE MMM dd yyyy HH:mm:ss 'GMT'Z
. The "BST" bit is ignored, and it's based on assumption that there's always "GMT" before offset.
解析它的方法之一是:EEE MMM dd yyyy HH:mm:ss 'GMT'Z
。“BST”位被忽略,它基于偏移前总是有“GMT”的假设。
回答by Alistair A. Israel
I would parse out and interpret the time zone information separately, then use that to construct the Date/Calendar object in the proper time zone.
我会分别解析和解释时区信息,然后使用它在适当的时区构造日期/日历对象。
The following code seems to work well enough with your example:
以下代码在您的示例中似乎运行良好:
String source = "Wed Aug 17 2011 09:57:09 GMT+0100 (BST)";
String tzid = "GMT" + source.substring(28, 31)
+ ":" + source.substring(31, 33);
TimeZone tz = TimeZone.getTimeZone(tzid);
// if (tz == null) ?
SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");
f.setTimeZone(tz);
Date date = f.parse(source);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(date);
Prints "Wed Aug 17 08:57:09 UTC 2011"
.
打印"Wed Aug 17 08:57:09 UTC 2011"
。
A more sophisticated approach would be to use regex to extract individual parts ("+/-", "hh" and "mm") of the time zone offset.
更复杂的方法是使用正则表达式来提取时区偏移的各个部分(“+/-”、“hh”和“mm”)。
Alternatively, you can attempt to discern the 3-letter time zone id (the string in between (
and )
), and use the corresponding Java TimeZone
if it exists.
或者,您可以尝试识别 3 个字母的时区 ID((
和之间的字符串)
),并使用相应的 Java(TimeZone
如果存在)。
In your particular example, though, "BST"
resolves to Bangladesh Time which is GMT+0600 so you're better off with the numeric offset. "BST"
here should probably be taken as British Summer Time (GMT+0100). This can be important because numeric offsets do not indicate the use of daylight savings time, which can be in effect depending on the date.
但是,在您的特定示例中,"BST"
解析为孟加拉国时间,即 GMT+0600,因此您最好使用数字偏移量。"BST"
这里应该被视为英国夏令时 (GMT+0100)。这可能很重要,因为数字偏移并不表示夏令时的使用,夏令时可能会根据日期生效。
A more heuristic routine could take this into account and attempt to resolve the name first, but verify that the GMT offsets match, and fallback on the simple "GMT+hh:mm"
timezones otherwise.
一个更具启发性的例程可以考虑到这一点并尝试首先解析名称,但验证 GMT 偏移量是否匹配,"GMT+hh:mm"
否则回退到简单的时区。
回答by Yannick Chaze
If you can not find a pattern matching your use case, try:
如果找不到与您的用例匹配的模式,请尝试:
try{
new Date("Wed Aug 17 2011 09:57:09 GMT+0100 (BST)")
}catch(Exception e)
{
// Parse exception
}