Guna Tulips Guna Tulips - 5 months ago 195
Java Question

@JsonFormat with TemporalType.Date not formatting Date

I'm using a date colum in my application as the following,

@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone = "IST")
private Date date;


I'm using SpringData JPA. Now the problem is, while using


TemporalType.DATE


I can't get the date as dd/MM/yyyy format.

If I use


TemporalType.TIMESTAMP instead of TemporalType.DATE


I can get the Date as expected. But


hibernateJpaVendorAdapter.setGenerateDdl(true);


option in JPA, creates table column with type
DATETIME
. But I need to create the column with type
DATE
.

Is there any solution for this scenario. Please help me to find the solution. Thanks in advance..

Answer

If You use java.util.Calendar instead of Date, You can get the format as u expect..

@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone = "IST")
private java.util.Calendar date;