
@[toc]
一、介绍
这段代码用于验证一个字符串是否符合"小时:分钟"(如"14:30")的24小时制时间格式,并严格校验小时和分钟的取值范围(00-23和00-59)。
二、代码
/**
* 检测一个字符串是否是时间格式,检测字符串是否符合"小时:分钟"的时间格式
* @param str 请求字符串
* @return 执行结果
**/
public static boolean isValidDate(String str) {
boolean convertSuccess = true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
try {
format.setLenient(false);
format.parse(str);
} catch (Exception e) {
convertSuccess = false;
}
return convertSuccess;
}

重要信息


