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)
Ilya
source share