public static boolean isValidDate(String dateStr, String formatStr) {
boolean convertSuccess = true;
if(StringUtils.isBlank(dateStr)){
convertSuccess = false;
}
SimpleDateFormat format = new SimpleDateFormat(formatStr);
try {
//设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
format.parse(dateStr);
} catch (ParseException e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}
调用
boolean flag = DateUtils.isValidDate(memberUpdateInfo.getBirthday(),"yyyyMMdd");