10 Replies Latest reply: Nov 22, 2007 5:14 PM by 807603 RSS

    Valid date

    807603
      Hi!
      I'm a beginner in java programming. I try to create a small application in order to get to know how to use java and learn new skills. I've got a problem with writing a proper code for a valid date. I have a project called Fridge. Virtual Fridge :) I've got some items and methods that allow a user to create, remove these items, display the fridge contents and so one. I want my fridge to check if the validity date of a product that a user types in e.g. creating a new product in a fridge is in appropriate format. I have spent a lot of time modifying different code I found in the Internet, but when I run my application and type a date always there is an exception. I just cant handle this. Does anyone have any idea what should i change in this code?
      private String readDate(String prompt) throws Exception {
                String userResponse = "  /  /    ";
                
                //display the prompt
                System.out.print(prompt);
                
                // wait for user input and read the date provided by the user
                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      
                {
                try{
                     userResponse = input.readLine();
                     
                     DateFormat formatter =     DateFormat.getDateInstance(DateFormat.SHORT);
                     formatter.setLenient(false);
                     formatter.parse(userResponse);
                
                     }
                catch(Exception e)
                     {
                          throw new Exception("Wrong date! " + e.getMessage());
                     }
                return userResponse;
                    }
      }
        • 1. Re: Valid date
          807603
          The trouble with using DateFormat.getDateInstance(DateFormat.SHORT) is that it's probably not the format you're assuming. use SimpleDateFormat:

          http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html
          • 2. Re: Valid date
            807603
            You should probably get in three distinct inputs, year, month, and date (preferably in integers).

            Then you can create a GregorianCalendar-object and check if the month and date are between actual minimum and actual maximum.

            Whoa, I now see that you take a string XXXX / YY / ZZ. In that, you can take String.split("/"), which gives you the array [" XXXX" ," YY" ," ZZ "].

            You can take String.trim() on each element to remove whitespace and than you can use Integer.parseInt(input[0]), Integer.parseInt(input[1]) and Integer.parseInt(input[2]).

            Hope this helps.
            • 3. Re: Valid date
              807603
              import java.text.*;
              import java.util.*;
              
              public class ParseExample {
                  public static void main(String[] args) throws ParseException {
                      String input = "2007-12-25";
              
                      String pattern = "yyyy-MM-dd";
                      DateFormat df = new SimpleDateFormat(pattern);
                      Date date = df.parse(input);
              
                      System.out.println(date);
                  }
              }
              I find it odd that you're returning a String, not a Date.
              • 4. Re: Valid date
                807603
                I'm sorry I do not understand that :/
                As I said I'm a begginer in java and I do not know how to implement this SimpleDateFormat. I tried but it doesn' t work.
                I'm not sure if you understood me - I just want the program to tell the user that the date e.g. 23/23/2008 is not a proper date. In fact now, every date that I type in is invalid for the program. What is more I'm not sure if the word in brackets here is correct - formatter.parse(userResponse);
                • 5. Re: Valid date
                  807603
                  As I said I'm a begginer in java and I do not know how to implement this SimpleDateFormat
                  You don't implement it -- you just use it.
                  • 6. Re: Valid date
                    807603
                    OK. Thanks for all of your replies. I think I have to learn more before I use the SimpleDateFormat... I do not know how to create the code.
                    • 7. Re: Valid date
                      807603
                      ania_j wrote:
                      OK. Thanks for all of your replies. I think I have to learn more before I use the SimpleDateFormat... I do not know how to create the code.
                      See reply #3 for a typical use.
                      • 8. Re: Valid date
                        807603
                        I tried this one but it doesnt match my code I created to readDate. I do not know how to connect it because i have two inputs - one that should be read and one that should be parsed. Could you please check this code:
                             private String readDate(String prompt) throws Exception {
                                  String userResponse = "";
                                  
                                  //display the prompt
                                  System.out.print(prompt);
                                  
                                  // wait for user input and read the date provided by the user
                                  BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                            
                                try {
                                       userResponse = input.readLine();
                                       String pattern = "dd/mm/yyyy";
                                     DateFormat df = new SimpleDateFormat(pattern);
                                     Date date = df.parse(input);
                                  } catch (Exception e) {
                        
                                  throw new Exception("Wrong user input! " + e.getMessage());
                                  }
                         
                        
                        
                                return userResponse;
                             }
                                  
                             }
                        • 9. Re: Valid date
                          807603
                          Date date = df.parse(userInput);
                          • 10. Re: Valid date
                            807603
                            Also, month is M and minute is m:

                            String pattern = "dd/MM/yyyy";