Thursday, April 24, 2008

Spring Framework - Date Field in a SimpleFormController

  1. Override the initBinder method.
  2. Create a SimpleDateFormat, e.g. MM/dd/yyyy HH:mm:ss where M is month, d is day, y is year, H is hour(0-23), m is minute, and s is second.
  3. Set the date format lenient false, which means inputs do not have to match the format exactly
  4. Create a CustomDateEditor(dateFormat, true), where true means empty allowed.
  5. Register the editor to Date.class.

1 comment:

Omar Vázquez Mayorga. said...

this can be the code.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(
dateFormat, false));