Thursday, May 17, 2012

Why doesn't parsing using SimpleDateFormat produces expected results?

One reason can be...

by default SimpleDateFormat parses strings in a lenient way. However, this may result in unexpected output. And, in occasions where parsing is used as a mechanism of enforcing date format validation, lenient parsing may parse a date which is in an invalid format.

To avoid this, lenient parsing should be switched off as follows:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat.setLenient(false);

Ref: http://ch-express.blogspot.com/2008/03/simpledateformat-is-lenient.html

No comments: