Apache DateUtils parses random date

494 Views Asked by At

Date date = DateUtils.parseDate("1243334-03332-284555", "yyyy-MM dd");

The Apache DateUtils was able to parse the above random date even though specifying the date format. Does anyone know what is the reason behind it ??

For my use case, the above date is an invalid date. How to make date util give an error on parsing this invalid date ??

1

There are 1 best solutions below

0
On BEST ANSWER

It seems that this method is lenient when parsing dates. In fact, the docs explicity say so ("The parser will be lenient toward the parsed date.").

You probably want to use DateUtils.parseDateStrictly(data, format) instead.

Date date = DateUtils.parseDateStrictly("1243334-03332-284555", "yyyy-MM dd");