Regex is overkill
To work with the date, no need to worry about regex .
Just try to parse with one format, catching the expected exception. If an exception is actually thrown, try parsing with a different format. If an exception is thrown, then you know that the input is unexpectedly absent in any format.
java.time
You are using old inconvenient time classes, which are now superseded by the java.time framework built into Java 8 and later. The new classes are inspired by the highly successful Joda-Time framework, designed as its successor, similar to the concept, but re-created. Defined by JSR 310 . Extended in the ThreeTen-Extra project. See Oracle Tutorial .
LocalDate
New classes include one, LocalDate , for date values ββonly without the time of day. Just what you need.
Formatters
Your first format may be the standard format ISO 8601 , YYYY-MM-DD. This format is used by default in java.time.
If this first parsing attempt fails because the input is not in ISO 8601 format, a DateTimeParseException is DateTimeParseException .
LocalDate localDate = null; try { localDate = LocalDate.parse( input ); // ISO 8601 formatter used implicitly. } catch ( DateTimeParseException e ) { // Exception means the input is not in ISO 8601 format. }
Another format should be specified by a coded template, similar to what you do with SimpleDateFormat. Therefore, if we catch the exception on the first try, do the second parsing attempt.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM-dd-yyyy" ); LocalDate localDate = null; try { localDate = LocalDate.parse( input ); } catch ( DateTimeParseException e ) { // Exception means the input is not in ISO 8601 format. // Try the other expected format. try { localDate = LocalDate.parse( input , formatter ); } catch ( DateTimeParseException e ) { // FIXME: Unexpected input fit neither of our expected patterns. } }