PopupDateField and Unparseable Date Exception

Despite setting the data format to “yyy-MM-dd” and seeing the correct format on display, the PopupDateField returned date value is always in a “full” format. e.g., "
Thu Apr 07 22:01:10 BST 2011
". Consequently, my validator keeps throwing error and complaining, rightly, that the format: “Thu Apr 07 22:01:10 BST 2011” is unparseable:


java.text.ParseException: Unparseable date: "Thu Apr 07 22:01:10 BST 2011"
at java.text.DateFormat.parse(Unknown Source)
at com.xoftsystems.odms.web.validator.CalendarValidator.isValidString(CalendarValidator.java:189)
at com.xoftsystems.odms.web.validator.AbstractBasicValidator.isValid(AbstractBasicValidator.java:199)
at com.vaadin.data.validator.AbstractValidator.validate(AbstractValidator.java:57)
at com.vaadin.ui.AbstractField.setValue(AbstractField.java:487)
at com.vaadin.ui.DateField.setValue(DateField.java:525)
at com.vaadin.ui.DateField.changeVariables(DateField.java:433)

Where is this format coming from and how do I override it? Mind you I am already using the ‘handleUnparsableDateString’ method but to no avail.

Thanks

It looks like the Date object is turned into a String at some point with toString(). The error seems to be coming from your code, which as a suspicious-looking name “isValidString()”, which I suppose takes the date as a String parameter?

The following validator that works purely on Date objects works:

    public void validate(Object value) throws InvalidValueException {
        Date dateValue = (Date) value;
        Date now = new Date();
        
        if (dateValue.before(now))
            throw new InvalidValueException("Before now");
    }

Marko Grönroos,

Thanks for the hint: I see your point about ‘toString()’ but why is it returning ‘Thu Apr 07 22:01:10 BST’ i.e., “EEE MMM dd HH:mm:ss z yyyy” no matter which other format I set with ‘setDateFormat(String dateFormat)’, say “dd-MM-yy”?

We extend ‘com.vaadin.data.validator.AbstractValidator’ but use apache Validators API and I now use the following which works:


public boolean isValidString(String value) {

		org.apache.commons.validator.routines.CalendarValidator validator  = 
		new org.apache.commons.validator.routines.CalendarValidator();
		List<String> list = Arrays.asList(PATTERNS);
		for(String pattern : list){
				if(validator.isValid(value,pattern)){
					return true;
				}
		}			
		return false;
	}

where PATTERNS is:


private static final String[] PATTERNS = new String[]
  {

		"dd MMM yyyy",
		"d MMM yyyy",
		...

		"EEE MMM dd HH:mm:ss z yyyy"
	};