How do I make sure that the input date has the correct month and day? ex 2021-99-99, obviously there is no such month 99 day 99. How do I make sure the input month number is within 12 and the correct days corresponded with the month?
Minimal reproducible code example:
SimpleDateFormat dateInput = new SimpleDateFormat("yyyy-MM-dd");
// In the real world the string will be entered by the user
String strDate = "2021-99-99";
try {
Date date = (Date) dateInput.parse(strDate);
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
} catch (ParseException e) {
System.out.println("Wrong format");
} catch (DateTimeParseException e) {
e.printStackTrace();
System.out.println("Invalid date");
}
Expected result: an exception and an error message being printed from either of the catch
blocks.
Observed output:
2029-06-07
It’s no error message and it’s also not the date that the user entered.