java利用正则表达式判断输入的日期格式为yyyy/MM/dd

public static void main(String[] args) throws ParseException{

String str1 = "2012-05-15";

String str2 = "2012/05/15";

if( zzbd(str1) ){

System.out.println("str1 have match!");

}else{System.out.println("str1 don't match!");}

if( zzbd(str2) ){

System.out.println("str2 have match!");

}else{System.out.println("str2 don't match!");}}

public static boolean zzbd(String s)

 {

Pattern pattern = Pattern.compile(\\d{4}/\\d{2}/\\d{2});

Matcher matcher = pattern.matcher(s);

if (matcher.find())

{

return true;

}else {

return false;

}

}

}

 

 

输出结果

str1 don't match!

str2 have match!

你可能感兴趣的:(java,web)