-2

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 ");

        }
    }
}

}

Noah Joe
  • 9
  • 1
  • Possible duplicate of [Checking the validity of a date](http://stackoverflow.com/questions/4528047/checking-the-validity-of-a-date) – Pavel Kovalev Sep 23 '16 at 03:53
  • Not a duplicate. That linked Question is practical while this Question is a homework assignment to practice coding the entire algorithm. – Basil Bourque Sep 23 '16 at 15:00

1 Answers1

0

Do you have to implement it from scratch? If not (what I hope), look at this:

Java: Check the date format of current string is according to required format or not

Community
  • 1
  • 1
  • 1
    Provide explanation with link as the link may turn obsolete in future – Balwinder Singh Sep 23 '16 at 02:01
  • Im a beginner at programming and yeah we pretty much have to do it from scratch. We cant use any of the DateFormat stuff. Were only in Chapter three which uses a lot of the if and else statements – Noah Joe Sep 23 '16 at 02:09