This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Call setLenient(false). By default, most DateFormat implementations are lenient. That means that they allow things like February 30th, or even a thirteenth month. It will just wrap these around - February 30th is March 2nd (or 1st in leap years), the thirteenth month will be January of the next year.
In your case it will use 2010 as the number of days in September 2017 (or 17, don't really know). It will overflow quite a few years I reckon
By turning leniency off you are prohibiting this behaviour. February 30th, a thirteenth month, or September 2010th 2017 will all cause errors.
Ah, more people to tell about the other parse method!
DateFormat.parse(String) uses DateFormat.parse(String, ParsePosition) internally, and throws an exception if parsing fails:
As for the two returns from my code. The first return statement is the basic form; it will only check if the date String starts with a valid date. If the pattern is yyyy-MM-dd then it will consider 2010-09-17abc as valid. The second form also checks if the parsing consumes all of the String. With the given example, date.length() is 13 whereas parsing stops at 10. Since 10 != 13 it will return false.
This non-strict parsing allows you to combine multiple formatters with one string. The ParsePosition gets updated, so a next Format would start at position 10, ignoring the preceding text.