Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be from 1 to a value that is appropriate for the given month. September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29. The remaining months all have 31 days each. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.

Respuesta :

Answer:

// here is code in java.

import java.util.*;

// class definition

public class Main {

   // main method of the class

   public static void main(String[] args) throws Exception {

       // Scanner object to read date

       Scanner in = new Scanner(System.in);

       System.out.print("Enter a date in format mm/dd/yyyy to check:");

       // read the date from user

       String inp_date = in .next();

       // split the date with "/" delimeter

       String[] ip = inp_date.split("/");

       // find month from date

       int inp_month = Integer.parseInt(ip[0]);

       // find day from date

       int inp_day = Integer.parseInt(ip[1]);

       // find year from date

       int inp_year = Integer.parseInt(ip[2]);

       // boolean variable

       boolean flag = true;

       // print the input date

       System.out.println("Your Entered date was: " + inp_date);

       

       // check month is valid or not

       if (inp_month < 1 || inp_month > 12)

       {

           

           System.out.println("Entered Date is invalid:");

           System.out.println("The input month  is not from 1 - 12.");

           flag = false;

           

       }

       //check day is valid or not

       else if (inp_day < 1) {

           

           System.out.println("Entered Date is invalid:");

           System.out.println("The value of day is less than 1.");

           flag = false;

       }

       // check days in those month which has 30 days

       else if ((inp_month == 4 || inp_month == 6 || inp_month == 9 || inp_month == 11) && inp_day > 30) {

           

           System.out.println("Entered Date is invalid::");

           System.out.println("This month have only 30 days, but Entered day is greater than 30.");

           flag = false;

           

       }

       // if month is February then check leap year and days

       else if (inp_month == 2 && ((inp_year % 4 == 0) && (inp_year % 100 != 0))) {

           if (inp_day > 29) {

               

               System.out.println("Entered Date is invalid::");

               System.out.println("In a leap year, only 29 days in February.");

               flag = false;

           

           }

       }

       // if month is February and year is non-leap

       else if (inp_month == 2 && inp_day > 28) {

           

           System.out.println("Entered Date is invalid:");

           System.out.println("In a non-leap year, only 28 days in February.");

           flag = false;

       }

       // if all are valid

       if (flag)

           System.out.println("Entered Date is valid:");

   }

}

Explanation:

Read the date from user in mm/dd/yyyy format with the help of Scanner object. Extract day, month and year from the date. Then check whether month is between 1-12 or not and print appropriate message. After that check day is less than 1 or not, if not then print reason. Check the days in those month which has 30 days in it.If month is February then check for leap year and day is greater than 29 then print reason. Else if month is February and year is non-leap then check days whether it is greater than 28 or not. In last If value of flag is true then print date is valid.

Output:

Enter a date in format mm/dd/yyyy to check:2/30/2012                                                                          

Your Entered date was: 2/30/2012                                                                                              

Entered Date is invalid::                                                                                                    

In a leap year, only 29 days in February.