【问题标题】:Joda Time and timezone offsetsJoda 时间和时区偏移
【发布时间】:2011-08-06 11:40:06
【问题描述】:

我在 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();

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

  • 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。 那么有什么问题呢? 我想抵消以反映格林尼治标准时间偏移量。 我也认为我上面的值是错误的:意味着2006-11-11-09:00 不是“日本”的时区,而是“美国/阿拉斯加”的时区。

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

最好的问候, 弗洛里安

【问题讨论】:

    标签: jodatime


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多