2

I am looking for solution with my current problem. I have a classic POJO mapped by Hibernate with Date variable.

@Entity
@Table(name="record")
public class Record implements Serializable {

    @Column(name = Record.COLUMN_CREATED)
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd.MM.yyyy hh:mm")
    private Date created;

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }
}

This entity is saved in MySQL database in TIMESTAMP variable (2013-09-25 22:13:18.000).

I am using Spring form to show data saved in my POJO.

<form:form method="post" commandName="record" action="record/update.htm">
  <table>
    <tr>
      <td><form:label path="<%=Record.COLUMN_CREATED%>"><spring:message code="administration.record.created"/>:</form:label></td>
      <td><form:input path="<%=Record.COLUMN_CREATED%>"/></td>
    </tr>
  </table>  
</form:form>

My problem is, when I want to edit this POJO and I send it into spring form to show, I get Date as 2013-09-25 22:13:18.000, exactly in the same format as MySQL timestamp. But I want to get this Date formated according the pattern, which I set using @DateTimeFormat annotation.

Can someone tells me, what am I doing wrong ? Many thanks, Ondrej.

EDIT: I have the @InitBinder already, but it works only when I am creating a new object through the form, so it cast String to Date. But when I want to fill the form inputs with existing data in DB, it doesnt work.

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy h:mm");
    sdf.setLenient(true);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}

I am using Spring 3.1.1.RELEASE without Joda time, just ordinary java.util.Date.

<mvc:annotation-driven/> in dispatcher-servlet.xml is set properly. When I use debugger I can see, that while creating the new object from form input values a method setAsText(String text) of CustomDateEditor is used. I expect that the second method, String getAsText() will be used for formating Data to String while filling the form inputs. But, it doesnt !

s0vet
  • 375
  • 4
  • 8
  • 22

1 Answers1

3

The @DateTimeFormat(pattern="dd.MM.yyyy hh:mm") annotation is basically saying that when you get a String in the particular pattern, convert it into

java.util.Date, java.util.Calendar, java.long.Long, or Joda Time fields.

In your case, it's a java.util.Date.

When you do something like

<spring:message code="administration.record.created"/>:</form:label>

You're just calling toString() on the created Date object. The Date class internally uses its own format when returning a String in toString() and that's what you see.

Consider using the fmt:formatDate tag from JSTL.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Hey, you overlooked something. `:` is just a label, which is not important at this time. The date value which I am trying to format is in ``. As I know, I cannot use fmt:formatDate on this spring form input, because Spring do the magic while filling this field. – s0vet Sep 26 '13 at 05:35
  • I think I have the same problem as here http://stackoverflow.com/questions/18163404/spring-mvc-date-format-with-forminput – s0vet Sep 26 '13 at 05:46
  • If I should simplify my question - how to format date in spring form:input field ? – s0vet Sep 26 '13 at 05:58
  • @s0vet No, with the solution above you would need to use the html `form` with `fmt:formatDate`. The `@InitBinder` solution seems to be what you want though. – Sotirios Delimanolis Sep 26 '13 at 12:17