java中的正则表达式检查模式和长度
我想检查给定日期的模式(yyyy-mm-dd hh:mm:ss)。 在下面的代码中,我想检查除yyyy-mm-dd hh:mm:ss之外的任何内容,如果其他内容或多或少,则应为false。有没有更好的方法来检查模式,这样我就不必单独检查日期的长度了
public class CheckNumber {
public static void main(String ...args){
CheckNumber cn1=new CheckNumber();
Boolean b=cn1.test("2011-10-12 08:29:56");
System.out.println(b);
}
public static boolean test (String s){
Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
Matcher matcher = pattern.matcher(s);
if (matcher.find()){
return true;
}
return false;
}
}
共 (0) 个答案