日期时间格式的正则表达式
亲验可用,含闰年验证
String el = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; //yyyy-MM-dd HH:mm:ss
//String el = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)"; //yyyy-MM-dd
Pattern p = Pattern.compile(el);
Calendar c = Calendar.getInstance();
//验证正则表达式是否正确
for (int i=0;i<200000;i++){
c.add(Calendar.DATE,1);
if(!p.matcher(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")).matches()){
System.out.println("格式错误\t"+DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss"));
}
}
//验证异常
String checkValue = "2008-02-30 00:00:00";
Matcher m = p.matcher(checkValue);
if(m.matches()){
System.out.println("格式正确");
}else{
System.out.println("格式错误");
}