SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?

前端 未结 7 1805
野的像风
野的像风 2020-11-27 08:06

Here is an example:

public MyDate() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy/MM/d\");
    sdf.setLenient(false);
    St         


        
相关标签:
7条回答
  • 2020-11-27 08:40

    Java 8 LocalDate may be used:

    public static boolean isDate(String date) {
    try {
            LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
            return true;
        } catch (DateTimeParseException e) {
            return false;
        }
    }
    

    If input argument is "2011/12/12aaaaaaaaa", output is false;

    If input argument is "2011/12/12", output is true

    0 讨论(0)
提交回复
热议问题