Search code examples
spring-data-jpatimezonetimestamp-with-timezonesql-timestamp

Setting timezone = "Asia/Kolkata" in @jsonformat and storing it in java.sql.timestamp converts 12 pm to 1 pm timesatmp to 00 am in spring hibernate


I am using

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss", timezone = "Asia/Kolkata")
private Timestamp startDateTime;

to store timestamp comes in json as a string. But the problem is it converts time between 12 pm to 1 pm into 00 am. E.g. 2021-10-25 12:30:00 gets converted to 2021-10-25 00:30:00. Any help will be appreciated. Thank you.


Solution

  • The root cause of the problem is that you have used h instead of H. Note that h is used for 12-hour time format (i.e. time with AM/PM marker) while H is used for 24-hour time format. So, the solution to your problem is to change the format to dd-MM-yyyy HH:mm:ss.

    In case, you want the AM/PM marker in the time, change the format to dd-MM-yyyy hh:mm:ss a.