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