首页 文章

Joda Time时区模式的格式错误无效

提问于
浏览
1

我不明白为什么以下几行代码不适用于Joda Time:

DateTime now = new DateTime();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.
                                              forPattern("yyyyMMddhhmmss Z");
System.out.println(dateTimeFormatter.print(now));
DateTime d = x.parseDateTime("200906031633 -0300");

我收到此错误:

java.lang.IllegalArgumentException:格式无效:“200006031633 -0300”格式错误为“-0300”org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)

对我来说奇怪的是 System.out.prinln(dateTimeFormatter.print(now)); 很好并且根据模式打印: 20110131101805 +0100

问题是什么?从我在Joda Time's pattern syntax上看到的,这种模式似乎是正确的 .

谢谢!

2 回答

  • 2

    首先,如果你希望它解析那个值,你的模式应该使用“HH”而不是“hh” . 其次,您还需要在值中包含秒数 .

    例如 .

    DateTime d = dateTimeFormatter.parseDateTime("20090603163300 -0300");
    
  • 2

    问题是你错过了 200906031633 -0300 中的秒数 . 如果我添加秒数,它可以工作,如下所示:(200906031633 "00" -0300)

    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmss Z");    
    DateTime d = dateTimeFormatter.parseDateTime("20090603163300 -0300");
    

相关问题