Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SimpleDateFormat Parse problem

Status
Not open for further replies.

fabienc

Programmer
Mar 5, 2007
3
AU
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:

Code:
    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
     */
    public int 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;
            } else if (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.
 
I don't know whether there is a way to solve that problem.

But in my opinion, these key-by-key-validators are annoying for users.

Guess there is a 17-Jun-1970 which should be a 17-Jan-1970.
A lot of users would place the cursor behind the u, and delete it, to make an a at that position, but in between there is a invalid 17-Jn-1970.

Or think of replacing the whole month: 17--1970 - invalid.

don't visit my homepage:
 
Use 3 pull-down menu's with Month, Day and Year, and when they select a month & year you can update their range of choices for the day...
 
I don't see the usability problem validating on key stroke: you can show it in red until a valid date is entered.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top