/**
* 根据时间 和时间格式 校验是否正确
* @param length 校验的长度
* @param sDate 校验的日期
* @param format 校验的格式
* @return
*/
public static boolean isLegalDate(int length, String sDate,String format) {
int legalLen = length;
if ((sDate == null) || (sDate.length() != legalLen)) {
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
try {
Date date = formatter.parse(sDate);
return sDate.equals(formatter.format(date));
} catch (Exception e) {
return false;
}
}
/**
* @Desc: 使用例子
**/
public static void main(String[] args) {
String yearMonthday = "2023-06-06";
System.out.println("YearMonthDay: "+isLegalDate(yearMonthday.length(),yearMonthday,"yyyy-MM-dd"));
}
标签:sDate,Java,String,验证,format,校验,return,length,格式 From: https://www.cnblogs.com/hungryquiter/p/17492734.html