Joda时间和时区抵消

时间:2011-08-06 11:40:07

标签: jodatime

我在Joda Time中遇到时区偏移问题。我认为这只是一个你理解的问题。

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

// 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();

当我解析并打印具有偏移设置的日期时,这可以正常工作。 E.g:

  • 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不是“日本”的时区,而是“美国/阿拉斯加”的时区。

我希望我的问题很明确。 有没有人对我的不理解有答案:)?

祝你好运, 弗洛里安

1 个答案:

答案 0 :(得分:1)

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

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