传入对应的时间格式,然后判断该时间是否是一个合法的时间。
public static boolean isValidDate(String str) {boolean convertSuccess = true;// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");try {// 设置lenient为false.否则SimpleDateFormat会比较宽松地验证日期,比如/02/29会被接受,并转换成/03/01format.setLenient(false);format.parse(str);} catch (ParseException e) { // e.printStackTrace();// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对convertSuccess = false;}return convertSuccess;}