public static void main(String[] args){
//System.out.println(isValidDate("2020-16-13"));
System.out.println(isValidDate("2020-10-13 14:88:09"));
}
public static boolean isValidDate(String str) {
boolean convertSuccess = true;
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}