开发者

Date validation is not working

Date validation is not working properly. If day = 90, month = 1 and year = 1990 The validation does not work. I开发者_开发百科t switches the date to march, with no validation. How can I validate it?

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

Date date;

try {
    date = formatter.parse(day + "/" + month + "/" + year);
} catch (ParseException e) {
    error = Boolean.TRUE;
    errorMessage = "Invalid date";
    return null;
}


Look at the setLenient() method, which stupidly defaults to true.

Nonetheless, I think there are still some issues even with it set to false. You might want to consider looking at JodaTime which makes this much easier.


You are missing something . Simpledataformat will just check for data format. It will roll over date if the date is invalid (feb 30th becomes march 1st). So once you check the format also check if the date is same.

(!sdf.format(date).equals(dateString)) { errorMessage = "The date that you provided is invalid."; return false; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜