Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a string from the keyboard and tests whether it conta

ID: 3551700 • Letter: W

Question

Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and 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 moth. September, April, June, and November each have 30 days. February has 28 days except for the 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.

Explanation / Answer

/* read a date from a string from the keyboard to determine if date is in mm/dd/yyyy format.

*/
import java.util.Scanner;

public class DateFormat

{

public static void main(String [ ] args)

{

   Scanner inp = new Scanner(System.in);

   System.out.println("Enter the date in mm/dd/yyyy format. ");

    int mm;

    int dd;

    int yy;

    int date = inp.nextInt();

    boolean isLeapYear;

        if ((mm <1) && (mm >12))

            System.out.println( date + " is not valid month must be between 1 and 12.");
       if (( mm = 4) && ( dd <=30 ))
            System.out.println( date + " is a valid date.");
            }
                else
                    System.out.println(date + " is not valid date must have 30 or less days. ");
         if (date = mm + "/" + dd + "/" + yy);
            System.out.println( date + " is a valid date format. ");
        }
        else{
            System.out.println( date + " is not valid must be in dd/mm/yyyy format.");

            if (yy %100 ==0) {
          if (yy % 400 ==0){
              isLeapYear = true;
          }  
          else{
              isLeapYear = false;
          }
        }
        else{
            if (yy %4 ==0){
             isLeapYear = true;
            }
            else{
             isLeapYear = false;

           if (isLeapYear)
           if ((mm ==2) && (dd <= 29 ))
            System.out.println(date + " is a valid date.");
           }
           else{
               System.out.println( date + "is not valid month must have 29 days or less.");
            if ((mm ==2) && (dd<=28))
                System.out.println( date + " is a valid date.");
            }
            else{
                System.out.println( date + "is not valid must have 28 days or less. ");
}
}


                                                                                  OR


public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Valid? " + isValidDate(scanner.nextLine()));
}

private boolean isValidDate(String inDate) {

    if (inDate == null)
      return false;

    //set the format to use as a constructor argument
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    if (inDate.trim().length() != dateFormat.toPattern().length())
      return false;

    dateFormat.setLenient(false);

    try {
      //parse the inDate parameter
      dateFormat.parse(inDate.trim());
    }
    catch (ParseException pe) {
      return false;
    }
    return true;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote