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; }
精彩评论