2

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}";
    }
5
  • can you please share toString implementation? Commented Jan 14, 2013 at 17:09
  • @Patton, please check my toString implementation in the edit. Commented Jan 14, 2013 at 17:15
  • instead of usind @DateTimeFormat, I would suggest you to try using @Temporal(TemporalType.TIMESTAMP) docs.oracle.com/javaee/6/api/javax/persistence/Temporal.html Commented Jan 14, 2013 at 17:21
  • @Temporal(DATE) doesnt work. The solution is to use a custom serializer as explained below. Commented Jan 14, 2013 at 20:42
  • I actually use @Temporal(TemporalType.TIMESTAMP) in my application and in case if I want the date in a specific format then I use SimpleDateFormat to format it in the way I need Commented Jan 15, 2013 at 5:11

1 Answer 1

4

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)

@JsonSerialize(using=JsonDateSerializer.class) 
public Date getDate() {     
   return date; 
} 

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.

/**
 * Used to serialize Java.util.Date, which is not a common JSON
 * type, so we have to create a custom serialize method;.
 */
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException {

        String formattedDate = dateFormat.format(date);

        gen.writeString(formattedDate);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Peter, i'm gonna try the code in the blog post. I'm pretty optimistic it will work.
Peter - it worked. That was very quick, many thanks! I've marked your answer as "Accepted". Much Obliged.
According to the javadocs: "Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally." Because of this, I'd recommend instantiating a new SimpleDateFormat inside the serialize method, or synchronizing access to the format method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.