Incorrect format parsing format for JodaTime

String dateString = "20110706 1607"; DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern("YYYYMMDD HHMM"); DateTime dateTime = dateStringFormat.parseDateTime(dateString); 

Stacktrace Result:

 Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "201107206 1607" is malformed at " 1607" at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:644) at org.joda.time.convert.StringConverter.getInstantMillis(StringConverter.java:65) at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:171) at org.joda.time.DateTime.<init>(DateTime.java:168) ...... 

Any thoughts? If I truncate a string to 20110706 with the pattern "YYYYMMDD", it works, but I need both hour and minute values. What is strange is that I can convert Jodatime DateTime to a string using the same "YYYYMMDD HHMM" template without any problems

Thanks for watching

+4
source share
1 answer

Look at your template - you specify "MM" twice. This may not be right. This will try to parse the same field (month in this case) twice from two different bits of text. What would you expect to win? Do you want to:

 DateTimeFormat.forPattern("yyyyMMdd HHmm") 

Check out the documentation for DateTimeFormat to see what it all means.

Please note that although calling toString with this pattern will create a string, it will not call the string you want. I would not be surprised if the output even turned on β€œYYYY” and β€œDD” because of the case, although I can’t check it right now. At least you will have a month twice instead of the minutes appearing at the end.

+11
source

All Articles