20

I have hibernate entity and a bean:

@Entity
public class GeneralObservation {
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    Date date;

    @Column
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

also I have

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

and

        form:input
                id = "datepicker"
                name="date"
                itemLabel="date"
                path="newObservation.date"

When I go to my url I see:

my form

How can I force it to have mm/DD/yyyy format? Thanx

incomplete-co.de
  • 2,137
  • 18
  • 23
Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206

4 Answers4

23

You can use fmt:formatDate jstl tag:

<fmt:formatDate value="${yourObject.date}" var="dateString" pattern="dd/MM/yyyy" />
<form:input path="date" value="${dateString} .. />
yname
  • 2,189
  • 13
  • 23
  • 1
    Yes, sorry for misrepresentation. In my application I have same situation, but date format is correct (like I specify in DateTimeFormat annotation before field). And not only presentation of field is correct: I have no @InitBinder method and all works correct. Maybe I have newer version of spring library (3.1.1.RELEASE)? – yname Aug 11 '13 at 18:16
8

The solution is to put
<mvc:annotation-driven/> to mvc-dispatcher-servlet.xml and also xmlns:mvc="http://www.springframework.org/schema/mvc" to the begining of xml file.

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
5

in my code I use the binder in this way:

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
Gray
  • 115,027
  • 24
  • 293
  • 354
Oibaf it
  • 1,842
  • 16
  • 9
-2

In HTML5 there is input type="date", same thing in Spring (Chrome , Opera, and I this Safary too, but not yet in Firefox

<form:input type="date" ... >
Hassen Ch.
  • 1,693
  • 18
  • 31