日期格式校验


/**
* 校验日期格式
*
* @param str
* @return
*/
private static boolean isValidDate(String str) {
boolean convertSuccess = true;
//判断字符串长度是否为10位
if (str.length() == 10) {
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
// 设置lenient为false.
// 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
Date dateStr = format.parse(str);
if (dateStr.after(format.parse("2019-09-26"))) {
convertSuccess = false;
}
} catch (Exception e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
} else {
convertSuccess = false;
}
return convertSuccess;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM