SWPM seeks pretty...
Posted on 2000-05-03
way to validate/convert dates in a JTextField.
(Sorry I couldnt resist)
Right now I have a pretty ugly method.
I use a generic filter to make sure input only includes specific characters (ie: 0123456789/.-) . I then use a focusLost() event to attempt to parse the input string into a date format. Once I have that I can format the date into the string I need for the output.
When focus is lost I want to convert it to MM/dd/yyyy. No problem there once I have a java.util.Date. My problem is taking any known input and converting it to a java.util.Date. Some examples:
12/31/00 123100 12312000 12.31.00 12.31.2000 12-31-00 12-31-2000
which is by no means an exhaustive list.
DateFormat doesnt seem to have a way to test if a given string is a compatible date unless you try to parse which throws an exception if its not like the input pattern. Ive thought of nested try/catch blocks (but like I said im looking for a prettier way).
I know everyone (and their dog) has probably done this at one time or another, and what I have works for most things (But its real darn ugly). Any one have a method they could share?
(An extra 10 points for the definition of SWPM :))