Help parsing a date with SimpleDateFormat

Hi!

I have implemented an editable combobox where the user can type some dates. The date he types should be of the form "dd-MMM-yy" i.e "12-JAN-07"

While the user types something I have a keylistener that calls the following method:

private String DEFAULT_DATE_PATTERN ="dd-MMM-yy";

private DateFormat DEFAULT_DATE_FORMAT =new SimpleDateFormat(DEFAULT_DATE_PATTERN);

/** Checks if the selected value is part of an allowed date value,

* or whether the input is invalid.

*

* @param selectedValue I User text in the text field.

* @return INVALID_VALUE if user entered something invalid,

* INCOMPLETE_VALUE if potentially valid text,

* ALLOWED_VALUE if complete calendar value

*/

publicint validateEntry(final Object selectedValue){

int result = EditableUpDownComboBox.INVALID_VALUE;

int len = DEFAULT_DATE_PATTERN.length();

Date date =null;

//the string should be of the form DD-MMM-YY (01-JAN-07)

if (isValueInstanceOfType(selectedValue)){

String value = (String)selectedValue;

if (value.length() > 0){

while (date ==null && len > 0){

String pattern = DEFAULT_DATE_PATTERN.substring(0, len);

DateFormat DATE_FORMAT =new SimpleDateFormat(pattern);

date = DATE_FORMAT.parse(value,new ParsePosition(value.length()-1));

if (date ==null){

--len;//let's shorten the date format and try again

}

}

}

if (len == DEFAULT_DATE_PATTERN.length()){

//Valid date

result = EditableUpDownComboBox.ALLOWED_VALUE;

}elseif (len >= value.length()){

//Partial date

result = EditableUpDownComboBox.INCOMPLETE_VALUE;

}

}

return result;

}

As you can see in order to match what the user types I shorten the pattern and do the parse again. I problem I am facing is that when the loops is at the "dd-M" or "dd-MM" stage, it will be looging for a number but once it becomes "dd-MMM" it will then be looking for a word. Hence:

12-1 or 12-11 is will be accepted and I dont want to! only 12-J, 12-JA should be!

Any help or a better way to do this would be greatly appreciated.

[3473 byte] By [fcoutela] at [2007-11-27 9:56:42]
# 1
Either don't check every character, or don't use SimpleDateFormat for the char-by-char validation, as that's not what it's intended for.I'd vote for the first option.
jverda at 2007-7-13 0:26:53 > top of Java-index,Java Essentials,New To Java...
# 2
I decided to use a regular expression test when the first two letters of the month are typed.
fcoutela at 2007-7-13 0:26:53 > top of Java-index,Java Essentials,New To Java...