0

Basically as you can see from the code, I want the user to enter a date, (start date & end date) but I want to make sure that the system doesn't let he user type in something else. and the user should be able to type it again. Where am I going wrong?

    System.out.println("Enter your date here: "); 
    Scanner scan = new Scanner (System.in);

    UserInput = scan.nextLine();

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new SimpleDateFormat("dd-MM-yyyy").parse(UserInput);

    while (true) {
        if (!isValidDate(date)){
            System.out.println(dateFormat.format(date));
            break;
        }
        else { 
            System.out.println("That is not a valid date! Please enter again (dd-mm-yy): ");
        UserInput = scan.nextLine();
        break;
        }
    } 
private static boolean isValidDate(Date date) {
    // TODO Auto-generated method stub
    return false;
}

and also if I will finally manage to do this? how can I add it an array saved in another class? my array are like this

arrayUsers[0] = new User("User", "One", 1234, 1234, 0); 
arrayUsers[1] = new User("User2", "Two" , 4321, 4321, 0);

I would like to add these "start date & date" here, but after 0 at the end of array. Thanks

6
  • what is in isValidDate ? Commented Mar 6, 2014 at 3:38
  • 1
    what is this isValidDate? check if this return true when date is valid or otherwise. Also you should use dateFormat.setLenient(false), it will give you parse exception if invalid date is entered. Commented Mar 6, 2014 at 3:38
  • By default, if the user enters invalid date, then parse method will throw ParseException. IMO, you handle that exception with appropriate message. Your title says about date comparison but your actual question is different. Commented Mar 6, 2014 at 3:43
  • It didn't work at the beginning, and it came up with an error. Then when I went to fix it it added a method at the bottom and it's empty. Commented Mar 6, 2014 at 4:08
  • possible duplicate of Java Date validation Commented Mar 6, 2014 at 5:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.