javaapacheapache-commonsapache-commons-dateutils

Apache DateUtils parses random date


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 ??


Solution

  • 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");