说实话,虽然写了很多年代码,但水平一直很一般。比如这次写一个日期格式验证居然前前后后改了四五次。把经历写下来,只是为了让自己考虑事情更全面一些。 需求:因为系统有很多日期格式,所以日期验证函数的输入是一个日期字符串和一个格式字符串。格式字符串用的是Java定义的格式(参考地址)。 刚开始写时,觉得很简单,直接就写了下面的代码。 1 2 3 4 5 6 7 8 9 10 11 12 public static boolean isDate(String dttm, String format) { boolean retValue = false; if (dttm != null) { SimpleDateFormat formatter = new SimpleDateFormat(format); try { formatter.parse(dttm); retValue = true; } catch (ParseException e) { } } return retValue; } 写好之后,一测试,发现2012/12/43这种日期居然也返回True,于是查资料,设置转化时不进位formatter.setLenient(false);之后好用了,代码为 1 2 3 4 5 6 7 8 9 10 11 12 13 public static boolean isDate(String dttm, String format) { boolean retValue = false; if (dttm != null) { SimpleDateFormat formatter = new SimpleDateFormat(format); formatter.setLenient(false); try { formatter.parse(dttm); retValue = true; } catch (ParseException e) { } } return retValue; } 写到这里,以为万事大吉了。但之后的测试却发现,当日期有分隔符时,只会转换前一部分,比如2012/12/43ABCD也会返回True。想了一下,觉得先转为日期型,再转为字符串,再比较两者是否相等,但马上就否决了这个方案,因为客户要求的是不严格的日期形式,如格式为yyyy/MM/dd,输入2012/1/2也需要验证通过。然后考虑了一下,觉得先用正则表达式做一次验证,然后再验证是否是日期型。代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public static boolean isDate(String dttm, String format) { boolean retValue = false; if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) { return retValue; } String regFormat = format; regFormat = regFormat.replaceAll("(^')|('$)", ""); regFormat = regFormat.replaceAll("'([^'])", "$1"); regFormat = regFormat.replace("''", "'"); regFormat = regFormat.replace("\\", "\\\\"); regFormat = regFormat.replaceAll("[MdHmsS]+", "\\\\d+"); regFormat = regFormat.replaceAll("[y]+", "\\\\d{1,4}"); if (!dttm.matches("^" + regFormat + "$")) { return false; } SimpleDateFormat formatter = new SimpleDateFormat(format); formatter.setLenient(false); try { formatter.parse(dttm); retValue = true; } catch (ParseException e) { } return retValue; } 上面的代码只对应了yMdHmsS,虽然对当时的系统已经足够了,但还是感觉不太爽,觉得应该有一种通用的方法。于是查Java的API,发现parse方法还有一个带参数的方法。理解了它的使用方法之后,把代码改成下面的样子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private static boolean isDate(String dttm, String format) { if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) { return false; } DateFormat formatter = new SimpleDateFormat(format); formatter.setLenient(false); ParsePosition pos = new ParsePosition(0); Date date = formatter.parse(dttm, pos); if (date == null || pos.getErrorIndex() > 0) { return false; } if (pos.getIndex() != dttm.length()) { return false; } return true; } 本来以为这样应该万事大吉了,但之后的测试又发现两个Bug。一个是,当输入的日期没有年份(需求是没有输入年份是默认当前年份)时,默认取的是1970年,这样的话,如果当年是闰年的话,2/29号就验证出错了;另一个是Java的日期和Oracle的日期大小不同,Oracle好像最大只支持到9999年,而Java可以有2万多年。所以代码又被改成了下面的样子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 private static boolean isDate(String dttm, String format) { if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) { return false; } if (format.replaceAll("'.+?'", "").indexOf("y") < 0) { format += "/yyyy"; DateFormat formatter = new SimpleDateFormat("/yyyy"); dttm += formatter.format(new Date()); } DateFormat formatter = new SimpleDateFormat(format); formatter.setLenient(false); ParsePosition pos = new ParsePosition(0); Date date = formatter.parse(dttm, pos); if (date == null || pos.getErrorIndex() > 0) { return false; } if (pos.getIndex() != dttm.length()) { return false; } if (formatter.getCalendar().get(Calendar.YEAR) > 9999) { return false; } return true; } 转载请注明:宇托小轩 ? 一个Java日期格式验证的历程 |
|