The following code…
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
df.setLenient(false);
Date date = df.parse("2012115");
…parses the string "2012115" to the Date equivalent of November 5th, 2012. I want it to fail parsing, because it is missing one day character. In other words, I only want the string "20121105" to evaluate to that date. Is there a way to enforce this, other than evaluating the length of the string?
I doubt you could do it using the formatter only. As a workaround, you may want to put a check on the date string length before parsing as below: