I have the program below which takes the user input from a string, parses into another format and then prints it again in another format.
How can I prevent the user from introducing a wrong format that would cause the date either to be displayed erroneously (ex. year 20222) or to cause a forward roll of the date (ex. introducing day 31 for a 30 day month causes to roll the month and set day to 01 of next month.).
ex. 2020-10-33 => 2020-11-3
I hope it makes sense!
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dater = reader.readLine();
Date newDate = dateFormat.parse(dater);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("LLL dd, yyyy");
System.out.println(dateFormat2.format(newDate).toUpperCase());
reader.close();
}
}