博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java检查字符串是否是合法的日期格式
阅读量:810 次
发布时间:2019-03-25

本文共 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/

你可能感兴趣的文章