开发者

Invalid format error with Joda Time timezone pattern

开发者 https://www.devze.com 2023-02-07 15:04 出处:网络
I don\'t understand why the followi开发者_如何学Pythonng lines of code are not working with Joda Time:

I don't understand why the followi开发者_如何学Pythonng lines of code are not working with Joda Time:

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

I get this error:

java.lang.IllegalArgumentException: Invalid format: "200006031633 -0300" is malformed at " -0300" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:683)

What is strange for me is that the System.out.prinln(dateTimeFormatter.print(now)); it's fine and prints according to the pattern: 20110131101805 +0100

What is the problem? From what I read on Joda Time's pattern syntax, the pattern seems correct.

Thank you!


First of all your pattern should use "HH" not "hh" if you expect it to parse that value. Secondly, you'll need to include seconds in the value as well.

E.g.

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


The problem is that you are missing seconds in the 200906031633 -0300. It works if I add seconds to that as shown below: (200906031633"00" -0300)

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

精彩评论

暂无评论...
验证码 换一张
取 消