I was trying to parse a date string using jodatime with a leading '+' before the yyyy
part. I expected an error to be thrown, but it actually did not throw an error. I got outputs that don't make any sense instead:
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 '+' sign is disallowed, or it should interpret +2013 as simply 2013, which is what it seems to do in the last case. But what is the deal with 20130 in case 2 and 3, and month = 10 in case 2?
If you want throw exception on +
sign you can use DateTimeFormatterBuilder, it is more flexible.
E.g. for format yyyyMMdd
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 pattern already present in standart patterns:
ISODateTimeFormat::basicDate()
EDIT
But there is strange behaviour in case of using 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 issue 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 joda library)
After going through joda-time code, I was able to narrow down the issue. It was being caused due to an anomalous increment in the code. I have opened an issue here. I also have a fix ready here. I'll raise a pull request once I get confirmation on is it is the right way to fix it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With