java用正则表达式判断字符串是不是时间

如题所述

具体代码如下:

1 public static boolean isValidDate(String str) {
2 boolean convertSuccess=true;
3      // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
4 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
5 try {
6      // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
7 format.setLenient(false);
8 format.parse(str);
9 } catch (ParseException e) {
10 // e.printStackTrace();
11 // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
12 convertSuccess=false;
13 }
14 return convertSuccess;
温馨提示:答案为网友推荐,仅供参考
相似回答