I’m using a java.util.Date in spring(3.1) data REST. How can I get the date to print in a human readable form? (e.g. MM/DD/YYYY)?
@Entity
public class MyEntity{
...
@Column(name="A_DATE_COLUMN")
@DateTimeFormat(iso=ISO.DATE)
private Date aDate;
..getters and setters
}
However when i print my entity(after overriding toString), I’m always getting the date as a long. It seems like @DateTimeFormat does not change the behaviour. I also tried different iso formats and that didnt help either.
"aDate" : 1320130800000
Here is my POM file entry for the spring data rest
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>1.0.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId></groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
Any help is much appeciated.
PS. Here is the toString Implementation
@Override
public String toString() {
return getClass().getName() + "{"+
"\n\taDate: " + aDate
+ "\n}";
}
looks like you will need to write a custom serializer to make Jackson (the JSON library spring uses under the hood) properly serialize the date out to text.
your getter will then look like this (where JsonDateSerializer is the custom class)
check out this blog post that includes code for the serializer. The serializer code is replicated here, but the explanation in the blog post may help.