将表示为包含时区名称 ('z') 的字符串的时间转换为 UTC 时间
我想将 "20000603163334 GST"
或 "20000603163334 -0300"
等字符串转换为 UTC 时间。问题是我的字符串中的时区可以是“一般时区”,我的意思是它们可以是 CET、GST 等字符串。而且我不知道如何转换这些时区。
由于这些字符串时区,我无法使用 Joda Time 的 DateTimeFormat.forPattern("yyyyMMddhhmmss z").withZone(DateTimeZone.UTC);
,因为根据文档: "无法解析时区名称('z')" 。
所以,我有一个问题是,您是否知道一种方法来解决 Joda Time 中的这一限制?如果可能的话,我更愿意使用 Joda Time,而不是标准的 Java API。
我认为可以用时区名称解决这个问题的另一个方法是使用 Java 的 SimpleDateFormat
。 所以我做了类似的事情:
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss z");
//f.setTimeZone(TimeZone.getTimeZone("UTC"));
f.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("UTC")));
Date time = f.parse("20000603163334 GST");
SimpleDateFormat
解析 String
(我在这里不关心有多个同名时区的问题 - 这个类解析什么这对我有好处)。
问题是我不知道如何将其从这里转换为 UTC。我该怎么做?
事实上,我将 f's
时区设置为 UTC(以上述两种方式)并没有帮助。我希望有人可以帮助我解决这个问题,我在 stackoverflow 上阅读了很多关于这个主题的问题和答案,但我还没有找到解决方案。
I want to convert Strings like "20000603163334 GST"
or "20000603163334 -0300"
to UTC time. The problem is that time zones in my strings can be 'general time zones', I mean they can be strings as CET, GST etc. etc. And I don't know how to convert these ones.
Because of these string time zones I can not use Joda Time's DateTimeFormat.forPattern("yyyyMMddhhmmss z").withZone(DateTimeZone.UTC);
, because according to the documentation: "Time zone names ('z') cannot be parsed".
So, one question I have is if you know a method to go around this limitation in Joda Time? I would prefer to use Joda Time, if possible, instead of the standard Java API.
Another in which I thought I can solve this problem with time zone's names is to use the Java's SimpleDateFormat
.
So I make something like:
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss z");
//f.setTimeZone(TimeZone.getTimeZone("UTC"));
f.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("UTC")));
Date time = f.parse("20000603163334 GST");
The SimpleDateFormat
parses the String
(I don't care here about the problem that there are multiple time zones with the same name - what this class parses it's good for me).
The problem is that I don't know how to convert it from here to UTC. How can I do this?
The fact that I set the f's
time zone to UTC (in both the two ways from above) doesn't help. I hope someone can help me fix this, I read a lot of questions and answers on this theme here, on stackoverflow, but I haven't found a solution yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决你问题的两种方法。第一个是将默认时区设置为 UTC:
我不确定这可能还会产生什么其他副作用。
我发现的第二个解决方案是使用不同的 SimpleDateFormat 进行输出。
使用两个 SimpleDateFormat 对象允许将输出放入 UTC 时间。以下是运行此代码的输出:
这可能是 Joda 不支持 3 字母区域 id 的原因。这是来自时区 ( http://download.oracle .com/javase/6/docs/api/java/util/TimeZone.html )JavaDoc。就 Joda 而言,我没有看到解决方法,但我对该库不太熟悉。
I found two solutions to your problem. The first was to set the default time zone to UTC:
I'm not sure what other side effect this might have.
The second solution I found was to use a different SimpleDateFormat for output.
Using two SimpleDateFormat objects allowed the output to be put in UTC Time. Here is the output from running this code:
Here may be the reason why Joda does not support 3 letter zone ids. This is from the TimeZone ( http://download.oracle.com/javase/6/docs/api/java/util/TimeZone.html ) JavaDoc. As far as Joda goes, I didn't see a workaround, but I'm not very familiar with that library.