public static boolean isValidDate(String s) {
try {
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy-MM-dd其中MM为大写
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 设置SimpleDateFormat的lenient为严格校验.
dateFormat.setLenient(false);
dateFormat.parse(s);
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
System.out.println("12".matches("^\\d+(\\.\\d+)?$"));
System.out.println("0.1".matches("^\\d+(\\.\\d+)?$"));
System.out.println("0.123456".matches("^\\d+(\\.\\d+)?$"));
System.out.println("-0".matches("^\\d+(\\.\\d+)?$"));
System.out.println("-1".matches("^\\d+(\\.\\d+)?$"));
System.out.println("-1.2".matches("^\\d+(\\.\\d+)?$"));
System.out.println("=====");
System.out.println(MainBiz.isValidDate("2015-02-02 15:46:21"));
System.out.println(MainBiz.isValidDate("2015-02-02"));
System.out.println(MainBiz.isValidDate("2015-02-02 32:46:21"));
System.out.println(MainBiz.isValidDate("2015-02-02 32:61:21"));
System.out.println(MainBiz.isValidDate("2015-02-02 32:46:60"));
}
【java正则】判断日期格式及非负浮点数
最新推荐文章于 2023-04-17 23:16:22 发布