import java.util.*;
public class DateChecker
These are my directions:
Write a Java application program that will allow the user to enter a date. The program will then check the date to verify whether or not it is a valid date.
Begin by asking the user to enter a date in the form, mm/dd/yyyy. This will be entered as a String, since the date contains slash marks.
After the date is entered, the program should check the date for validity. If the date is valid, the program should display the message "Valid date". If the date is not valid, the program should display the message "Not a valid date" along with a message to indicate why the date is not valid.
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.println("Enter a date");
String dateStr = kbd.nextLine();
if(dateStr.length() == 10){
if (dateStr.charAt(2) == '/' && dateStr.charAt(5) == '/'){
String mmStr = dateStr.substring(0, 2);
String ddStr = dateStr.substring(3, 5);
String yyyyStr = dateStr.substring(6);
int mm = Integer.parseInt(mmStr);
int dd = Integer.parseInt(ddStr);
int yyyy = Integer.parseInt(yyyyStr);
if (mm <= 12 && mm >=1){
if (((((mm == 1) && (dd == 31)) || ((mm == 3) && (dd == 31)) || ((mm == 4) && (dd == 30)) || ((mm == 5) && (dd ==31)) || ((mm == 6) && (dd == 30)) || ((mm == 7) && (dd == 31)) || ((mm == 8) && (dd == 31)) || ((mm == 9) && (dd == 30)) || ((mm == 10) && (dd == 31)) || ((mm == 11) && (dd == 30)) || ((mm == 12) && (dd == 31)))))
{
System.out.println("Vailid date.");
}
else if (mm == 2){
if ((yyyy%4==0 && yyyy%100!=0) ||yyyy%400==0)
{
if (dd == 29){
System.out.println("Valid date");
}
else {
System.out.println("Not a valid date \nDay is not valid");
}
}
else {
if (dd == 28){
System.out.println("Valid date");
}
else {
System.out.println("Not a valid date \nDay is not valid");
}
}
}
else
{
System.out.println("Not a valid date \nDay is not valid");
}
}else {
System.out.println("Not a valid date \nMonth is not valid");
}
}
else {
System.out.println("Not a valid date \nIncorrect format");
}
if (dateStr.length() > 10){
System.out.println("Too many characters in the date");
}else {
System.out.println("Too few characters in the date ");
}
}
}
}