Search code examples
hibernatedatetimespring-data-jpahibernate-annotations

How to force JPARepository to display timestamp in yyyy-MM-dd HH:mm:ss.000 format


I am maintaining code written in a few years ago, using JPARepository to access the database. The creationDate columns in the database are defined as datetime.

The legacy version of the code will extract values from creationDate and display it as "creationDate": "2015-03-27T14:44:05.897+0000".

I had to modify several tables in the database. In the process, I upgraded Spring Boot to version 1.4.3, which brought in a newer version of Hibernate also.

Now when I run the code (which did not change anything with dates), the JSON now display the column as "creationDate": 1427467445897 . This breaks code that calls my web service.

My code has:

@Column(name="CreationDate", nullable=true) 
private java.sql.Timestamp creationDate;

and there is no annotation on the getter function. What must I do to get the JSON date displayed again as "2015-03-27T14:44:05.897+0000"?


Solution

  • I suspect this has nothing to do with spring-data but rather with Jackson since repository don't display anything.

    @Column(name="CreationDate", nullable=true)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    private java.sql.Timestamp creationDate;
    

    Look here to get the pattern right.