教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 Java日期格式验证几个实例程序

Java日期格式验证几个实例程序

发布时间:2016-11-30   编辑:jiaochengji.com
教程集为您提供Java日期格式验证几个实例程序等资源,欢迎您收藏本站,我们将为您提供最新的Java日期格式验证几个实例程序资源
本文章来给大家介绍一个日期格式验证实例,这是转一个同学的写的日期验证程序,希望此方法转过来给大家有帮助哦。

需求:因为系统有很多日期格式,所以日期验证函数的输入是一个日期字符串和一个格式字符串。格式字符串用的是Java定义的格式(参考地址)。
刚开始写时,觉得很简单,直接就写了下面的代码。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8344')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8344>

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);之后好用了,代码为

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5826')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5826>

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也需要验证通过。然后考虑了一下,觉得先用正则表达式做一次验证,然后再验证是否是日期型。代码如下

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6290')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6290>

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方法还有一个带参数的方法。理解了它的使用方法之后,把代码改成下面的样子

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7218')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7218>

 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万多年。所以代码又被改成了下面的样子

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2878')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2878>

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;
}

您可能感兴趣的文章:
常用js验证代码大全(Email、手机号码、身份证号码、文件类型等)
Java日期格式验证几个实例程序
PHP验证身份证格式
JS验证日期格式YYYY-mm-dd的代码一例
php 正则验证日期时间格式实例代码
javascript 验证日期的函数
php 获取今日、昨日、上周、本月的起始与结束时间戳
jquery.validate使用攻略 第二部
JS验证身份证有效性的实例代码
php验证判断一个日期的格式是否正确

[关闭]
~ ~