Validate date less than current date

Asked

Viewed 1,878 times

1

My question is as follows. I have a JSP form for the registration of students with several attributes, one of them is the date of birth. Until then I was registering the date only validating the format (dd/MM/yyyy) and with Annotations. But now, I need to actually validate the date in a way that the user can’t register a later date than today and none of the ideas that I had and implemented in JPA worked. Someone knows a way to do it?

  • 1

    Could you demonstrate where you are doing the validation and what should happen if the validation fails? In particular, know if the validation is done in JSP, javascript, Servlet, taglib, spring, CDI or whatever. Reginaldo Rigo posted a reply that shows how to compare dates, but without you making it clear which is the place where you do the validation, which Annotation you use where, it is difficult to give a more complete answer than that.

1 answer

0

Do so:

public static boolean comparaDatas(String psDate1, String psDate2) throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
        Date date1 = dateFormat.parse(psDate1);
        Date date2 = dateFormat.parse(psDate2);
        if(date2.after(date1)) {
            return true;
        } else {
            return false;
        }
    }
  • 1

    You can replace the if(date2.after(date1)) { return true; } else { return false; } by just return date2.after(date1);

  • Yes. Of course.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.