0

I have a Java application that reads information about a new user to insert into an SQL database. When the user is to input a new user's date of birth, I want to program to be sure that the user has entered a date that is in the exact format YYYY-MM-DD.

I want to use the Scanner method next(Pattern pattern), but I'm not sure exactly how to create the necessary Pattern for what I'm trying to achieve. Here is what I have so far:

String date [] = null;
    while (date == null) {
        try {
            date = userInput.next(Pattern.compile(*your suggestion here*)).split("-");
        } catch (NoSuchElementException e) {
            System.out.print("Date must be in format YYYY-MM-DD: ");                
            date = null;
            userInput.nextLine();
            continue;
        }
    }

What should I put in *your suggestion here*?

Mike B
  • 75
  • 1
  • 7

1 Answers1

1

Use SimpleDateFormat as a date parser, not a regex.

djechlin
  • 54,898
  • 29
  • 144
  • 264