日期格式校驗


/**
* 校驗日期格式
*
* @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