6 Replies Latest reply: Mar 20, 2007 1:28 AM by 807599 RSS

    Get a date from a string.

      I have a method and this is the header
       public Date (String initialDateString)
      i have three instance variables - month, day, year.

      I want it to take the string and have it initialize it into a Date.

      so if the parameter was "1/2/1993"

      I wanted it to use a string.substring so
      month = initialDateString.substring (0 0)
      day = initialDateString.substring (2 3)
      year = initialDateString.substring(4)
      but then what if someone typed 01/02/1993

      i want it to still be able to read it without prompting the user to type it in a specific format.
        • 1. Re: Get a date from a string.
          The user is always going to have to input a date with some sort of formatting. It sounds like you are letting them enter it into a free text field. So what if they enter 01-02-2007?

          Some better options might be to use a calendar widgit and let the user pick a date. Or an easier option may be to give the user three seperate text fields that they can enter the day, month and year in seperatly. Or perhaps you could have three seperate drop down boxes from which the user can select the day, month and year.

          If you are determined to use a free text box, you could perhaps attempt to parse the input stirng, delimited by the "\" character, or the "/" character or the "-" character, and any other character you think a use might use to seperate the day, month and year.
          • 2. Re: Get a date from a string.
            so would I use that one string method that reads everything up to a specific point, i forgot which one it was, but for example have it read up to the first delimeter, then second? something like that?
            • 3. Re: Get a date from a string.
              A simple solution may be the split() method:
                  public static void main(String[] args) {
                      String text1 = "01/02/2007";
                      String text2 = "1/2/07";
                      String[] split = text1.split("/");
                      for (int i = 0; i < split.length; i++) {


              split = text2.split("/");
              for (int i = 0; i < split.length; i++) {

              Output is:

              • 4. Re: Get a date from a string.
                Also, I'm not familar with it, but you could probably use the java.util.regex API to check for multiple delimiters. e.g. is the string delimited by a "/" or a "\" or a "-". I think this should be possible, but I havn't used it before.
                • 5. Re: Get a date from a string.
                  ah allright, I'll trial and error multiple methods and that split() one to see which better suits it. Thanks :)
                  • 6. Re: Get a date from a string.

                    so i have a month , day, and year variable which are all ints.

                    i got a method called convert which converts a string into a Date object.

                    im just having problems with the constructor that calls them.

                    this is what the constructor looks like:
                    public Date (String initialDateString)     // explicit-value constructor
                            Date crDate = new Date();
                                 crDate = convert(initialDateString);
                            catch(DateException badDateString)
                                 System.out.println("Error in format");
                            month = month.crDate;
                            day = day.crDate;
                            year = year.crDate;
                        }   // end Date explicit-value constructor
                    the convert returns a Date() object.

                    oh and this is the DateException:
                    public class DateException extends Exception
                        //************** STATIC VARIABLES ************************************
                        public static final int INVALID_FORMAT  = 1,
                                                INVALID_DATE    = 2,
                                                EMPTY_STRING    = 3,
                                                NULL_STRING     = 4;
                        //************** INSTANCE VARIABLES *****************************
                        private int errorCode;
                        private String badDateString;
                        *    CONSTRUCTOR for the DateException class:
                        * Method: explicit value constructor 
                        * @param theErrorCode the Error code corresponding to this error.
                        * @param theBadDateString the String that contains the error.
                        * Postcondition: the exception has been set to the values in the
                        *                arguments.
                        public DateException (int theErrorCode, String theBadDateString)
                            errorCode = theErrorCode;
                            badDateString = theBadDateString;
                        } // end constructor
                        //************* METHODS *****************************************
                         * Method: getErrorCode
                         * @return the DateException's error code value.
                        public int getErrorCode()
                            return errorCode;
                        } // end method getErrorCode
                         * Method: getBadDateString
                         * @return the DateException's bad date string value.
                        public String getBadDateString()
                            return badDateString;
                        } // end method getBadDateString
                         * Method: getMessage
                         * @return A message String containing the bad date string and a 
                         *                description of the error.
                         *** NOTE: This function and the toString function are redundant, but
                         ***       both are provided because getMessage is a commonly used
                         ***       method with exceptions, and toString is automatically called
                         ***       when an exception is printed.
                        public String getMessage()
                            return this.toString();
                        } // end method getMessage
                         * Method: toString
                         * @return A String containing the bad string and a description
                         *                of the error.
                        public String toString()
                            String message = new String();
                            switch (errorCode)
                                case INVALID_FORMAT: message = 
                                            "Bad date format in " + badDateString + ".";
                                case INVALID_DATE: message = 
                                            badDateString + " is not a valid date.";
                                case EMPTY_STRING: 
                                            message = "The date String is \"\".";
                                case NULL_STRING: message = 
                                            "The date String is null.";
                                default: message = 
                                            "Unknown error on " + badDateString + ".";
                            return "DateException: " + message;
                        } // end method toString
                    } // end class DateException