Java 8 LocalDateTime is parsing invalid date

前端 未结 5 621
南旧
南旧 2020-11-27 07:04

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

5条回答
  •  迷失自我
    2020-11-27 08:01

    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.

提交回复
热议问题