Java 8 LocalDateTime is parsing invalid date

前端 未结 5 619
南旧
南旧 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 07:46

    LocalDateTime.parse will only throw an error if the String passed in contains invalid characters, a number of days exceeding 31 or a month exceeding 12.

    For example, if you modified your code as such:

    String dateString = "11:30:59 0zz2/31/2015";
    

    an exception would be thrown caused by the invalid 'zz' characters within your given date. As to why it's 'rounding-down' the date so to speak, that I don't know.

    Source: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-

提交回复
热议问题