/**
* 判断日期格式
* 2004-2-30 是无效的
* 2020-04-31 是无效的
* @param sDate
* @return
*/
public static boolean isLegalDate(String sDate , String format) {
if(sDate == null || isEmpty(format)){
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
try {
Date date = formatter.parse(sDate);
return sDate.equals(formatter.format(date));
} catch (Exception e) {
return false;
}
}
需求:根据输入的字符串和格式,判断是否符合需要的格式。
本文介绍了一种用于判断日期字符串是否符合特定格式的有效方法。通过使用SimpleDateFormat类,该方法能够检查输入的日期字符串是否为合法的日期,如'2004-2-30'和'2020-04-31'将被识别为无效。
1221

被折叠的 条评论
为什么被折叠?



