Aseem Bansal
Aseem Bansal

Reputation: 6962

Date parsing not failing with invalid format

The below piece of code should throw a parsing exception as per my understanding but it does not. Looked at the documentation but couldn't figure it out.

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.US);
dateFormat.setLenient(false);
dateFormat.parse("20160821_$folder$");

Upvotes: 1

Views: 171

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30819

This is what the javadoc says:

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

As long as it finds the match, it stops scanning further which seems to be the case here.

If you want strict checking, you can add RegEx mathing on top of this, to prevent such strings from getting parsed.

Upvotes: 2

Related Questions