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*?