Results 1 to 3 of 3
  1. #1
    getshum is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default SDF validating date input

    //ask for date code
    Scanner sd = new Scanner(System.in);
    sd.useDelimiter("/");
    System.out.println("Please enter the date in MM/DD/YYYY format:");
    String input = sd.nextLine();
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    sdf.setLenient(false);

    int month = 0;
    int day = 0;
    int year = 0;

    String fullDate = "";
    try
    {
    sdf.parse(input);
    sd = new Scanner(input);
    sd.useDelimiter("/");
    month = sd.nextInt();
    day = sd.nextInt();
    year = sd.nextInt();
    fullDate = + month + "/" + day + "/" + year;
    //System.out.println(fullDate);
    }
    catch (Exception err)
    {
    System.out.println("Invalid Date.");
    System.out.println(err);
    }
    I am a beginner java student in school so I dont know much :\

    So for this small block of code I am having trouble getting it to accept the year as EXACT 4 digits. for example, the user can type in 2/2/11 and my code won't catch it. I want the user to only be able to type 2/2/2011 or 02/02/2011 for example SO THEN the code is perfect. one more question is I want it to loop back to the beginning of the try statement if there a mistake that is caught.

    The reason I want it to be 2011 is because I am using a calendar.. and it affects the values if it isnt 2011 and is 11. which is below

    sd = new Scanner(input);
    System.out.println("Service date: " + fullDate);
    String svcDateIn = sd.nextLine();
    sd = new Scanner(svcDateIn);
    sd.useDelimiter("/");
    month = month - 1;
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, day);
    int n = cal.get(Calendar.DAY_OF_WEEK);
    System.out.println("Number of day is " +n);
    THANKS TO ANYONE THAT CAN HELP ME. MY BRAIN IS ABOUT TO EXPLODE!

  2. #2
    getshum is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    no one can see why my validation doesnt work??

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    From the API:
    "For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields."

    In other words you can't do it with just the SDF.
    The usual thing is to either check the string beifre formatting (split() and check the length of the bits you want to check), or limit what the user can enter by not actually allowing them to type in a date. That is, use a calendar pop-up, or a set of drop down selections, that sort of thing.

Similar Threads

  1. trouble validating 2 conditions
    By cottoneye1256 in forum New To Java
    Replies: 2
    Last Post: 08-23-2010, 07:53 PM
  2. Validating Jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 03:54 PM
  3. Validating url with regular expression??
    By kirtichopra2003 in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 02:29 PM
  4. validating account
    By ServletEst in forum Java Servlet
    Replies: 3
    Last Post: 08-24-2009, 02:11 PM
  5. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 09:10 AM

Posting Permissions

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