I am having issues with a custom component that I created. The component's function is to ensure that the date entered adheres to the format "^(\\d{2})/(\\d{2})/(\\d{4})$" The problem, though, is when the form that contains the component is submitted, the date is converted to the previous day's date. For example, if a date of 07/07/2009 is entered, the value that gets submitted is 07/06/2009 at 19:00 CDT. My code (2 classes) is as follows:

public class CustomDateTextField extends TextField {

    public CustomDateTextField(String id, IModel model, Class type) {
        super(id, model, type);
        add(new EzdecDatePicker());
    }

    public CustomDateTextField(String id, IModel model) {
        super(id, model);
        add(new EzdecDatePicker());
    }

    public CustomDateTextField(String id, Class type) {
        super(id, type);
        add(new EzdecDatePicker());
    }

    public CustomDateTextField(String id) {
        super(id);
        add(new EzdecDatePicker());
    }

    @Override
    public IConverter getConverter(Class<?> type) {
        return new StrictPatternDateConverter();
    }
}

public class StrictPatternDateConverter extends PatternDateConverter {

    public static final String REGEX_PATTERN =
            "^(\\d{2})/(\\d{2})/(\\d{4})$";

    public StrictPatternDateConverter() {
        super("MM/dd/yyyy", false);
    }

    @Override
    public Date convertToObject(String value, Locale locale) {
        final Pattern pattern = Pattern.compile(REGEX_PATTERN);
        if (StringUtils.isNotBlank(value) && !pattern.matcher(value).matches()) {
            throw new ConversionException("Invalid date format");
        }
        return super.convertToObject(value, locale);
    }
}

Reply via email to