I wanted to validate date in client side so I wrote the following code. But instead of getting an exception I am getting a proper date object for 31st of February date strin
It is not rounding down. February has never had 31 days, and it is impossible to use a validating date / time object to represent a day that doesn't exist.
As a result, it takes the invalid input and gives you the best approximation to the correct date (the last date of February that year).
SimpleDateFormat
inherits from DateFormat
which has a setLenient(boolean value)
method on it. I would expect that if you called setLenient(true)
prior to parsing, it would probably complain more, as detailed in the javadocs.