I'm getting a date keyed in by users and am having a problem understanding SimpleDateFormat. If the user types in a date in the format "mm/dd/yy" and SimpleDateFormat is set to SHORT everything works ok. If the user types in a date in "mm/dd/yyyy", then SimpleDateFormat gives me a ParseException regardless of the style (SHORT, MEDUIM, LONG, FULL). Is there a way to validate a user entered date which could be in one of the following three formats: mm/dd/yy, mm/dd/yyyy or yyyy/mm/dd where the slashes could also be dashes (mm-dd-yyyy). Do I need to write a string parsing routine which will strip the first two digits from the year if the year is 4 digits and re-arrange the string so that the year is in the last position? I am in local US.
Interesting, Is this an AWT or Swing application? If it is, you could Subclass TextArea or JTextArea to force the proper input. If it's a webpage, you could use drop down boxes that force the input. The last choice I would use would be to basically Factory the creation of the SimpleDateFormat Object. Look for qualifying attributes for each case, length, characters present etc and return the format String based on these characteristics. ------------------ Hope This Helps Carl Trusiak
Dates are so tricky that it's almost always a really bad idea to allow entry of dates as text. You may be in the US locale, but if a European or someone else who prefers the date in a different order enters a date in dd/mm/yyyy instead of the US-style mm/dd/yyyy your software can not tell (is 02/03/2001 Feb 3rd or March 2nd ??). If you have any form of GUI interface on your software, please consider using some sort of "calendar picker", so that people can only enter valid dates.
Thanks for your replies! I'm writing a servlet (using Tomcat) which gets a date keyed into a HTML form. Frank, I understand your point about international users. If I get the language from the HTTP header, maybe I can deduce the location (so that I can determine the order of the month and day). I think I will also list in the header portion of the HTML screen the required date format. The problem is that SimpleDateFormat doesn't allow dates of the format "mm/dd/yyyy". I think I'll have to write a routine to strip off the first two digits of the year if they enter it this way.
Joined: Jun 13, 2000
Originally posted by Steve Campbell: SimpleDateFormat doesn't allow dates of the format "mm/dd/yyyy".
Actually using the constructor SimplaDateFormat(String pattern) you can construct one that will work with what ever pattern you wih. For your example DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); Will allow it.
Joined: Dec 18, 2000
You're correct about SimpleDateFormat. The problem is that you have to know ahead of time which format your user will key in the date. The other issue I see is that there is no exception thrown when you parse (using SimpleDateFormat), so that if the user keys in 01/45/2001 (and the setLenient flag is set to false), no exception is thrown. For this reason I ended up using DateFormat.