乔达时间和时区偏移
我在乔达时间中遇到时区偏移问题。我认为这只是一个理解问题。
我有以下解析器和打印机:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否可以在设置默认时区之前实例化
DateTimeFormatter
或DateTime
?无论如何,我相信
withZone()
方法应该为您提供您正在寻找的格式化程序/解析器。Can it be that you instantiate
DateTimeFormatter
or aDateTime
before setting a default timezone?Anyway, I believe
withZone()
method should give you the formatter/parser you're looking for.