将表示为包含时区名称 ('z') 的字符串的时间转换为 UTC 时间

发布于 2024-10-19 11:44:35 字数 1105 浏览 0 评论 0原文

我想将 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2024-10-26 11:44:35

我找到了解决你问题的两种方法。第一个是将默认时区设置为 UTC:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

我不确定这可能还会产生什么其他副作用。

我发现的第二个解决方案是使用不同的 SimpleDateFormat 进行输出。

SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss z");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
Date time = f.parse("20000603163334 GST");
System.out.println(time);
System.out.println("(yyyyMMddhhmmss z):  " + f.format(time));
    
SimpleDateFormat utc = new SimpleDateFormat("yyyyMMddhhmmss z");
utc.setTimeZone(TimeZone.getTimeZone("UTC"));
    
System.out.println("(yyyyMMddhhmmss z):  " + utc.format(time));

使用两个 SimpleDateFormat 对象允许将输出放入 UTC 时间。以下是运行此代码的输出:

2000 年美国东部时间 6 月 3 日星期六 08:33:34

(yyyyMMddhhmmss z):20000603043334 消费税

(yyyyMMddhhmmss z):20000603123334 UTC

这可能是 Joda 不支持 3 字母区域 id 的原因。这是来自时区 ( http://download.oracle .com/javase/6/docs/api/java/util/TimeZone.html )JavaDoc。就 Joda 而言,我没有看到解决方法,但我对该库不太熟悉。

三字母时区 ID
与 JDK 1.1.x 的兼容性,一些
其他三字母时区 ID(例如
如“PST”、“CTT”、“AST”)也是
支持。然而,它们的用途是
已弃用,因为相同
缩写通常用于
多个时区(例如,
“CST”可能是美国“中央标准”
时间”和“中国标准时间”),以及
Java 平台只能
认出其中之一。

I found two solutions to your problem. The first was to set the default time zone to UTC:

TimeZone.setDefault(TimeZone.getTimeZone("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.

SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss z");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
Date time = f.parse("20000603163334 GST");
System.out.println(time);
System.out.println("(yyyyMMddhhmmss z):  " + f.format(time));
    
SimpleDateFormat utc = new SimpleDateFormat("yyyyMMddhhmmss z");
utc.setTimeZone(TimeZone.getTimeZone("UTC"));
    
System.out.println("(yyyyMMddhhmmss z):  " + utc.format(time));

Using two SimpleDateFormat objects allowed the output to be put in UTC Time. Here is the output from running this code:

Sat Jun 03 08:33:34 EDT 2000

(yyyyMMddhhmmss z): 20000603043334 GST

(yyyyMMddhhmmss z): 20000603123334 UTC

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.

Three-letter time zone IDs For
compatibility with JDK 1.1.x, some
other three-letter time zone IDs (such
as "PST", "CTT", "AST") are also
supported. However, their use is
deprecated because the same
abbreviation is often used for
multiple time zones (for example,
"CST" could be U.S. "Central Standard
Time" and "China Standard Time"), and
the Java platform can then only
recognize one of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文