乔达时间和时区偏移

发布于 2024-11-28 09:00:13 字数 1658 浏览 1 评论 0原文

我在乔达时间中遇到时区偏移问题。我认为这只是一个理解问题。

我有以下解析器和打印机:

// The formatter for the timezone
    DateTimeFormatter timezoneFormatter = new DateTimeFormatterBuilder().appendTimeZoneOffset(null, true, 2, 2)
                .toFormatter();

    // This formatter equals: yyyy-MM-dd
    DateTimeFormatter dhmsFormatter = ISODateTimeFormat.date();

    // Here a parser is created that parses a string of the form yyyy-MM-dd. Further the string may have
    // a timezone. withOffsetParsed makes the parser to respect the set timezone (if one is set)
    DATE_TIME_PARSER = new DateTimeFormatterBuilder().append(dhmsFormatter)
                .appendOptional(timezoneFormatter.getParser()).toFormatter().withOffsetParsed();

    // Here a printer is created that prints this dateTime in the form yyyy-MM-dd ZZ
    DATE_TIME_PRINTER = new DateTimeFormatterBuilder().append(dhmsFormatter).append(timezoneFormatter.getPrinter()).toFormatter();

当我解析和打印设置了偏移量的日期时,它按预期工作。例如:

  • 2006-11-11-09:00
  • 2004-12-01+00:00

这两个值的解析和打印如上面所写。

现在我的问题是: 我希望能够设置默认时区(即未设置时的默认偏移量)。 我会这样做: DateTimeZone.setDefault(DateTimeZone.forID("Etc/GMT+1"));

我现在的期望是 2006-11-11 被解析并打印为 2006-11-11+01:00,但实际上它打印2006-11-11-01:00

我认为这在某种程度上是正确的,因为这里 http://www.joda.org/joda-time /timezones.html 中写道 Etc/GMT+1 的标准偏移量为 -01:00。 那么出了什么问题呢? 我想要偏移以反映 GMT 偏移。 我还认为我上面的值是错误的:意味着 2006-11-11-09:00 不是“日本”的时区,而是“美国/阿拉斯加”的时区。

我希望我的问题很清楚。 有人能回答我的不理解吗:)?

此致, 弗洛里安

I have a problem with timezone offsets in Joda Time. I think it is simply a problem uf understanding.

I have the following parser and printer:

// The formatter for the timezone
    DateTimeFormatter timezoneFormatter = new DateTimeFormatterBuilder().appendTimeZoneOffset(null, true, 2, 2)
                .toFormatter();

    // This formatter equals: yyyy-MM-dd
    DateTimeFormatter dhmsFormatter = ISODateTimeFormat.date();

    // Here a parser is created that parses a string of the form yyyy-MM-dd. Further the string may have
    // a timezone. withOffsetParsed makes the parser to respect the set timezone (if one is set)
    DATE_TIME_PARSER = new DateTimeFormatterBuilder().append(dhmsFormatter)
                .appendOptional(timezoneFormatter.getParser()).toFormatter().withOffsetParsed();

    // Here a printer is created that prints this dateTime in the form yyyy-MM-dd ZZ
    DATE_TIME_PRINTER = new DateTimeFormatterBuilder().append(dhmsFormatter).append(timezoneFormatter.getPrinter()).toFormatter();

This works as expected when I parse and print dates that have an offset set. E.g.:

  • 2006-11-11-09:00
  • 2004-12-01+00:00

Those two values are parsed and printed as they are written above here.

Now my question:
I want to be able to set a default time zone (i.e. default offset when none is set).
I'll do that like this:
DateTimeZone.setDefault(DateTimeZone.forID("Etc/GMT+1"));

My expectation is now that 2006-11-11 is parsed and printed as 2006-11-11+01:00, but in fact it prints 2006-11-11-01:00.

I think this is somehow correct because here http://www.joda.org/joda-time/timezones.html it is written that Etc/GMT+1 has the standard offset of -01:00.
So what's wrong?
I want to offset to reflect the GMT-offset.
I also think that my values above are wrong: means 2006-11-11-09:00 is not the timezone of "Japan" but it is the timezone of "US/Alaska".

I hope my problem is clear.
Does anybody have an answer for my not-understanding :)?

Best regards,
Florian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

久伴你 2024-12-05 09:00:13

您是否可以在设置默认时区之前实例化 DateTimeFormatterDateTime

无论如何,我相信 withZone() 方法应该为您提供您正在寻找的格式化程序/解析器。

Can it be that you instantiate DateTimeFormatter or a DateTime before setting a default timezone?

Anyway, I believe withZone() method should give you the formatter/parser you're looking for.

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