0
LocalDateTime dateAndTime = LocalDateTime.now(); //gets current date and time
    
    String birthdateIn = cBirthdate.getText(); //gets birthdate input
    
    SimpleDateFormat mmddyyyy = new SimpleDateFormat("mm/dd/yyyy"); //mm/dd/yyyy formatter
    mmddyyyy.setLenient(false);
    
    try {
        Date stringToDate = mmddyyyy.parse(birthdateIn);
        bLabel.setForeground(Color.green);
        bLabel.setText("Valid Birthdate");
    }
    catch (ParseException e) {
        bLabel.setForeground(Color.red);
        bLabel.setText("Invalid Birthdate");
    }

This is the code I'm using to check for birthdate validity in JFrame, and while the checker works correctly, it validates year when I input a single integer instead of checking if it contains 4 integers. How do I correct it?

Also how do I extract the inputted birthdate to check if the user is below 18 years old?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kuromiu
  • 31
  • 7
  • 1
    let a `JFormattedTextField` do the job https://stackoverflow.com/questions/12655811/how-to-make-a-jtextfield-having-a-fixed-date-format – fantaghirocco Jan 10 '21 at 10:34
  • @fantaghirocco’s suggestion is fine. For how to use `JFormattedTextField` with java.time (the modern Java date and time API), see [my answer here](https://stackoverflow.com/a/61446403/5772882). – Ole V.V. Jan 10 '21 at 11:49

3 Answers3

4

I would recommend to use the new Date/Time API (java.time) instead of the legacy classes. Therefore, you can use a DateTimeFormatter with the necessary pattern to parse birthdateIn to a LocalDate object. Afterwards you can check whether the user is below 18 years by using LocalDate#isAfter in combination with LocalDate.now().minusYears(18).

Here is a working example:

String birthdateIn = "01/09/2003"; // change to check boundaries

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");

try {
    LocalDate birthdate = LocalDate.parse(birthdateIn, formatter);
    System.out.println("Valid Birthdate");
    
    if (birthdate.isAfter(LocalDate.now().minusYears(18))) {
        System.out.println("not 18 years yet");
    } else {
        System.out.println("18 years or older");
    }

} catch (DateTimeParseException e) {
    System.out.println("Invalid Birthdate");
}
Matt
  • 12,848
  • 2
  • 31
  • 53
1

java.time

You are already using LocalDateTime from java.time, the modern Java date and time API. I certainly recommend that you stick to java.time and avoid the old SimpleDateFormat class. It’s a notoriously troublesome class and in particular hopeless for validation (setting leniency false helps a little bit, but solves only one of the many problems).

You will probably want to validate the birth date in two ways:

  1. The string should be a valid date.
  2. The date should be within a reasonable range. For the birth date of a living person, say that it must not be more than 140 years ago and not in the future, for example.

Most of the code you need is already in the good answer by Matt, so I am not repeating.

Matt
  • 12,848
  • 2
  • 31
  • 53
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You can use this very well shaped regex that i modified for you to verify that the date you get is correctly shaped

birthdateIn.matches("^(((0[13578]|(10|12))/(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))/[0-9]{4}$")
AirlineDog
  • 520
  • 8
  • 21