乔达时间时区解析与地区/城市
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
System.out.println(
DateTimeZone.forID("Europe/Copenhagen")
);
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm dd MM YY Z");
System.out.println(
formatter.parseDateTime("19:30 29 8 11 Europe/Copenhagen")
);
}
}
我希望它能够解析哥本哈根时区的日期,但它失败了:
Europe/Copenhagen
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "19:30 29 8 11 Europe/Copenhagen" is malformed at "Europe/Copenhagen"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)
at Main.main(Main.java:13)
为什么?
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
System.out.println(
DateTimeZone.forID("Europe/Copenhagen")
);
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm dd MM YY Z");
System.out.println(
formatter.parseDateTime("19:30 29 8 11 Europe/Copenhagen")
);
}
}
I would expect this to to parse the date in Copenhagen timezone, and yet it fails with:
Europe/Copenhagen
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "19:30 29 8 11 Europe/Copenhagen" is malformed at "Europe/Copenhagen"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)
at Main.main(Main.java:13)
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 JodaTime DateTimeFormat javadocs 的
DateTimeFormat
您应该使用ZZZ
而不是Z
。它很容易被错过,因为该文档中的表格只显示 Z。页面下方是这样的,“区域:'Z' 输出不带冒号的偏移量,'ZZ' 输出带冒号的偏移量,'ZZZ' 或更多输出区域 ID。”
Looking at the JodaTime DateTimeFormat javadocs for
DateTimeFormat
you should useZZZ
notZ
.Its easy to miss since the table in that doc only shows Z. Down the page a bit is this, "Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id."
Joda-Time v2.0 仅添加了欧洲/哥本哈根等时区 ID 的解析
Parsing of time zone IDs like Europe/Copenhagen was only added in Joda-Time v2.0
我正在使用的解决方案(到目前为止似乎有效)是:
The solution I'm using, which seems to be working so far is: