views:

268

answers:

1

I am having problems trying to write the correct code to test whether a string from the keyboard contains a valid date with the correct amount of months, days for each particular month and format mm/dd/yyyy with adjustments for leap years here is what Ive done so far I dont think I am even close but any help would be greatly appreciated. Thanks in advance

/* 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. ");
+4  A: 

I think this will be helpful: http://www.javadb.com/check-if-a-string-is-a-valid-date

Basically, you don't really need to write the code to check whether the date is valid. You can use the SimpleDataFormat class from the java.text package, for example.

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;
  }
Bruno Rothgiesser