@DateTimeFormat not working properly

2019-07-15 09:54发布

问题:

I'm having issues with using the @DateTimeFormat Spring annotation. I've been researching a bit about it today, but for some reason I just can't get it to work.

@DateTimeFormat(pattern="MM/dd/yyyy")
@Column(name="my_date")
private Date myDate;

public Date getMyDate() {
    return this.myDate;
}
public void setMyDate(Date myDate) {
    this.myDate = myDate;
}

Right now the code is displaying the date and time as yyyy-MM-dd HH:mm:ss.S (i.e. 2012-01-11 00:00:00.0)

I read that you need Joda-Time added to your class path and I've done that. Am I missing something?

回答1:

You need to enable the processing of those annotations. The easiest way is with mvc schema...

<mvc:annotation-driven />

Joda Time is not required for formatting. @DateTimeFormat can be used on long, Date and Calendar fields. If you want to use it on Joda Time fields, you need to have 1.3 or higher on your classpath