7

How can I set the format for a Date in a text field with Spring MVC?

I'm using the Spring Form tag library and the input tag.

What I get now is something like this Mon May 28 11:09:28 CEST 2012.

I'd like to show the date in dd/MM/yyyy format.

davioooh
  • 23,742
  • 39
  • 159
  • 250

3 Answers3

8

register a date editor in yr controller :

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(LocalDate.class, new LocalDateEditor());
}

and then the data editor itself can look like this :

public class LocalDateEditor extends PropertyEditorSupport{

 @Override
 public void setAsText(String text) throws IllegalArgumentException{
   setValue(Joda.getLocalDateFromddMMMyyyy(text));
 }

 @Override
 public String getAsText() throws IllegalArgumentException {
   return Joda.getStringFromLocalDate((LocalDate) getValue());
 }
}

I am using my own abstract utility class (Joda) for parsing dates, in fact LocalDates from Joda Datetime library - recommended as the standard java date/calendar is an abomination, imho. But you should get the idea. Also, you can register a global editor, so you don't have to do it each controller (I can't remember how).

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • Thank you! that's definitely better than my solution. – davioooh May 28 '12 at 13:16
  • @davioooh Spring 3.0 + ? This is relevant chapter I think http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html "using propertyregistars" shows how to do globally – NimChimpsky May 28 '12 at 13:19
  • Yes, I'm using Spring 3.1, but I'm still new to it... (and to Spring Framework in general...) – davioooh May 28 '12 at 13:22
  • here how to set a global editor: http://stackoverflow.com/q/1268021/1061499 Very helpful! – davioooh May 28 '12 at 13:30
8

Done! I just added this method to my controller class:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}
davioooh
  • 23,742
  • 39
  • 159
  • 250
7

If you want to format all your dates without having to repeat the same code in every Controller, you can create a global InitBinder in a class annotated with @ControllerAdvice annotation.

Steps

1. Create a DateEditor class that will format your dates, like this:

    public class DateEditor extends PropertyEditorSupport {
     
    public void setAsText(String value) {
      try {
        setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
      } catch(ParseException e) {
        setValue(null);
      }
    }

    public String getAsText() {
      String s = "";
      if (getValue() != null) {
         s = new SimpleDateFormat("dd/MM/yyyy").format((Date) getValue());
      }
      return s;
    }

2. Create a class annotated with @ControllerAdvice (I called it GlobalBindingInitializer):

    @ControllerAdvice
    public class GlobalBindingInitializer {
     
     /* Initialize a global InitBinder for dates instead of cloning its code in every Controller */
     
      @InitBinder
      public void binder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateEditor());
      }
    }

3. In your Spring MVC configuration file (for example webmvc-config.xml) add the lines that allow Spring to scan the package in which you created your GlobalBindingInitializer class. For example, if you created GlobalBindingInitializer in the org.example.common package:

    <context:component-scan base-package="org.example.common" />

Finished!

Sources:

Kurt Bourbaki
  • 11,984
  • 6
  • 35
  • 53