I am working on an old java 7 environment that my work still uses and I need to validate user dates. The issue is that I need return an error if the user inputs an invalid date so I am trying to implement this answer using the java calendar object though right now if the user types the date 2001-02-31 the java date object converts the date to 2001-03-02 thus making the calendar never throw the exception to the user because the converted date is valid. I am wondering how do I get around the date being converted automatically being converted so its never converted?
Here is my code so far:
//formats the date for us
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date checkLDate = dateFormatter.parse(request.getParameter("loDate"));
out.print(checkLDate + "<br/>");
//checking to see if the dates are valid
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.setTime(checkLDate);
try {
cal.getTime();
//this print is just to debug where the code went
out.print("hello");
}
catch (Exception e) {
out.print("Invalid date");
}
Hopefully, that helps and thank you for all the help in advance