本文共 686 字,大约阅读时间需要 2 分钟。
代码如下:
/** * 检查日期格式是否合法 * * @param str 日期字符串 * @return boolean */ private static boolean isValidDate(String str) { boolean convertSuccess = true; // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写,可扩展到时分秒; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 format.setLenient(false); format.parse(str.intern()); } catch (ParseException e) { // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 convertSuccess = false; } return convertSuccess; }
转载地址:http://myvuk.baihongyu.com/