在Java

时间:2016-09-23 09:33:16

标签: java exception simpledateformat

我找到了很好的功能,它验证了日期String的格式和正确性。我想将其升级为仅验证>= 1900年。

所以这就是我发现的:

public boolean isDateValid(String date) {
    try {
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        df.setLenient(false);
        df.parse(date);
        return true;
    } catch (ParseException e) {
        return false;
    }
}

这是我的升级版本:

public boolean isDateValid(String date) {
    try {
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        df.setLenient(false);
        df.parse(date);
        Integer year = Integer.parseInt(date.substring(6, 10));
        if (year>= 1900)
            return true;
        else
            return false;
    } catch (ParseException e) {
        return false;
    }
}

所以我没有返回true,而是检查year变量是否大于或等于1900.问题是当我使用"12-12-1xxx"运行此函数时(编辑:或"12-12-1abc")。解析年NumberFormatExceptionString时会抛出int。绝对不应该发生因为ParseException应该先抛出,打破try {}块。

看起来第一个列表中的验证无法正常工作,因为它接受以数字开头的每个"yyyy"部分。然而,"12-12-xxxx"编辑:或"12-12-abcd")一切正常。

修改

在阅读时停止投票我的问题和焦点。问题非常明确:为什么新的SimpleDateFormat(" dd-MM-yyyy")。解析(" 12-12-1xxx")不会抛出ParseException?

4 个答案:

答案 0 :(得分:2)

据我所知,从javadoc开始,SimpleDateFormat将1作为年度的有效部分,并将解析它。相反,您可以尝试使用正则表达式验证日期。您可以在Regex to validate date format dd/mm/yyyy

找到一些示例

答案 1 :(得分:1)

parse方法的文档是:

  

从给定字符串的开头解析文本以生成日期。   该方法可能不会使用给定字符串的整个文本。

因为不需要使用整个字符串,所以" 12-12-1xxx"实际上被解析为" 12-12-1",你得到第1年的约会。

您可以使用parse方法的结果来获取年份,而不是使用子字符串。 getYear已弃用并返回1900年的偏移量,因此您可能希望先转换为CalendarLocalDate

public boolean isDateValid(String date) {
    try {
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        df.setLenient(false);
        Date parsed = df.parse(date);
        int year = parsed.getYear() + 1900;
        if (year >= 1900)
            return true;
        else
            return false;
    } catch (ParseException e) {
        return false;
    }
}

答案 2 :(得分:0)

我已查看您的功能,并查找以下详细信息。

如果您将在main函数中传递 12-12-1xxx,那么即使我打印由df.parse(date)转换的日期输出,它也会返回false 10月10日00:00:00 GMT 2。

所以你不会随时获得解析异常,

<强>建议

您可以通过Exception更改ParseException,也可以使用catch for NumberFormatException,如下所示。

 public boolean isDateValid(String date) {
    try {
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        df.setLenient(false);
        df.parse(date);
        Integer year = Integer.parseInt(date.substring(6, 10));
        if (year>= 1900)
            return true;
        else
            return false;
    } catch (Exception e) { // Use Exception or if you want ParseException then use below commented code
        return false;
    }
    /*catch (NumberFormatException nfe) { // Use this if you use ParseException
        return false;
    }*/
 }

答案 3 :(得分:0)

如果有人对代码的外观感兴趣,那么它就是:

public boolean isDateValid(String date, String format) {
    if (date.length()!=format.length())
        return false;
    try {
        DateFormat df = new SimpleDateFormat(format);
        df.setLenient(false);

        df.parse(date); // exception is not thrown if day and month is
                        // correct AND the first char of year is a digit

        // so if we have correct day and correct month
        // and we know the year has 4 chars we can try to parse it
        Integer year = Integer.parseInt(date.substring(6, 10));

        if (year>= 1900 && year<=2015) // here we know that the year is 4 digit integer
            return true;               // and we can limit it
        else
            return false;

    } catch (ParseException e) {
        return false;
    } catch (NumberFormatException e) {
        return false;
    }
}
相关问题