Strange jodatime behavior when parsing some date formats

I tried to parse a date string using jodatime with a leading "+" before the yyyy part. I expected the error to be thrown, but in fact it did not cause the error. I got results that make no sense:

 System.out.println(DateTimeFormat.forPattern("yyyyMMdd").parseDateTime("20130101")); // 2013-01-01T00:00:00.000+05:30 (Expected) (case 1) System.out.println(DateTimeFormat.forPattern("yyyyMMdd").parseDateTime("+20130101")); // 20130-10-01T00:00:00.000+05:30 (??? Notice that month changed to 10 also) (case 2) System.out.println(DateTimeFormat.forPattern("MMyyyydd").parseDateTime("01+201301")); // 20130-01-01T00:00:00.000+05:30 (??? At least month is fine this time) (case 3) System.out.println(DateTimeFormat.forPattern("MM-yyyy-dd").parseDateTime("01-+2013-01")); // 2013-01-01T00:00:00.000+05:30 (I expected an error, but this parsed correctly) (case 4) 

Can anyone explain why this is happening? I expect either an exception, which means that the โ€œ+โ€ sign is forbidden, or it should interpret 2013 as just 2013, which seems to do in the latter case. But what is the deal with the year 20130 in case 2 and 3, and the month = 10 in case 2?

+8
java datetime jodatime
source share
3 answers

After going through the joda-time code, I was able to narrow down the problem. This is caused due to abnormal code growth. I discovered the problem here . I also have a ready-made solution here . I will raise a pull request as soon as I get confirmation, this is the right way to fix it.

+1
source share

If you want to throw an exception on the + sign, you can use DateTimeFormatterBuilder , this is more flexible.
For example. for yyyyMMdd format

 DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendFixedDecimal(DateTimeFieldType.year(), 4) .appendMonthOfYear(2) .appendDayOfMonth(2) .toFormatter(); dtf.parseDateTime("19990101"); - parsed correctly dtf.parseDateTime("-19990101"); - throw exception dtf.parseDateTime("+19990101"); - throw exception 

also this template is already present in standard templates:

ISODateTimeFormat :: basicDate ()

EDIT
But there is a weird behavior when using the appendFixedSignedDecimal method:

 DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendFixedSignedDecimal(DateTimeFieldType.year(), 4) .appendMonthOfYear(2) .appendDayOfMonth(2) .toFormatter(); dtf.parseDateTime("19990101"); - parsed correctly dtf.parseDateTime("-19990101"); - parsed correctly (negative years) dtf.parseDateTime("+19990101"); - throw exception (???) 

I think this is a problem in Joda lib because

 DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendFixedSignedDecimal(DateTimeFieldType.year(), 4) .toFormatter(); 

works as expected

 dtf.parseDateTime("1999"); - parsed correctly dtf.parseDateTime("-1999"); - parsed correctly (negative years) dtf.parseDateTime("+1999"); - parsed correctly 

(This case is present in Unit Tests for the joda library)

+1
source share

this is not an answer, but may help ..:

  Case 1: 2013-01-01T00:00:00.000-05:00 Case 2: 20130-10-01T00:00:00.000-04:00 Case 3: 20130-01-01T00:00:00.000-05:00 Case 4: 2013-01-01T00:00:00.000-05:00 

Notice how case 2 changed the time.

I'm in ET timezone and use joda-time v2.3

0
source share

All Articles