Results 1 to 4 of 4
  1. #1
    Junki3JJC is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Taking user input as a date and validation!

    Hello there!
    I'm having a bit of a problem with my Java code. Basically for this part what I'm wanting to do is take in a user input, make this into a date, ensure it's a VALID date, and work out the number of days this is from a "starting date". Here's what I've got so far...

    Java Code:
    public static void getDate() throws IOException{
       //  prompt the user to enter a date
             System.out.print("Enter a date for prediction: ");
             //  open up standard input
             BufferedReader br = new BufferedReader(new InputStreamReader(;
             //  read the date from the command-line.
             try {
                userDate = br.readLine();
             } catch (IOException ioe) {
                System.out.println("IO error trying to read date.");
             String pattern = "yyyy/MM/dd";
             SimpleDateFormat sdf = new SimpleDateFormat();
             try {
                date =sdf.parse(userDate);
                //date = new GregorianCalendar(date).getTime();
             } catch (ParseException e) {
                System.out.println("Wrong date format. Please try again.");
             Date startdate = new Date("2009/01/01");
             int difInDays = (int) ((date.getTime() - startdate.getTime())/(1000*60*60*24));
             System.out.println("Difference in Days: "+difInDays);
    It's correctly working out the difference in days, but it just won't correctly validate the input (i.e. it accepts the 30th of February). I've also tried to use the GregorianCalender thing (you'll see where I've commented out) which I *think* would correctly verify the input, but I can't even get it to work. Any ideas?

    Cheers guys!

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    There are a lot utill class in web to check whether the date range is correct or not. google it and get suitable class . Before going to calculate the date difference validate your input date if that s true ahead further process otherwise print the error msg.
    (Living @ Virtual World)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    SimpleDateFormat has a lenient flag.
    Set it to false and it will reject that date.

  4. #4
    Junki3JJC is offline Member
    Join Date
    Mar 2011
    Rep Power


    Yep, you were right Tolls. Got it working by adding the line:

    Java Code:

Similar Threads

  1. Problem with taking input and printing it.
    By equal in forum New To Java
    Replies: 3
    Last Post: 11-11-2010, 10:02 PM
  2. Taking args consisting of a scanner input
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-27-2009, 09:16 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Taking input from Properties File
    By bugger in forum New To Java
    Replies: 4
    Last Post: 05-07-2008, 09:30 AM
  5. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 04:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts