LitterWalker
LitterWalker

Reputation: 321

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"?

Upvotes: 3

Views: 4623

Answers (2)

LitterWalker
LitterWalker

Reputation: 321

I found a comparable solution:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone="UTC")
public java.sql.Timestamp getCreationDate() {
    return creationDate;
}

I do not know if that annotation can be on either the data declaration or the getter function, but it is working on the getter function.

Upvotes: 0

ketrox
ketrox

Reputation: 929

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.

Upvotes: 4

Related Questions