Home » Java » java – Springboot getting UTC+1 instead of GMT when called from endpoint-Exceptionshub

java – Springboot getting UTC+1 instead of GMT when called from endpoint-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I actually have an issue with dates when they are called from an endpoint :

The date returned is one hour before UTC+1

But on the database the date is correct (UTC+1)

And when debugging the date from my object is also correct (UTC+1)

I have the following setUp :

Main.java :

@SpringBootApplication
@Slf4j
public class Main extends SpringBootServletInitializer
{
  @PostConstruct
  void started() {
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
  }


I have set Hibernate to the following timezone :

application.properties :

spring.jpa.properties.hibernate.jdbc.time_zone = Europe/Paris

I get the correct date before posting it :

Date before post

Date from database (took 4 minutes to post because the programm was on pause) :

Date from database

Date from postman when calling endpoint

enter image description here

Anyone has any idea on why the date when returned throught an endpoint seems to be a GMT ?

How to&Answers:

The comment from vickirk pointed that the problem was related to Jackson

You need to add a timezone to your JsonFormat annotation
Like this :

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss", timezone = "Europe/Paris")
@Column(name = "date_creation", nullable = true)
protected Date dateCreation;

The related issue can be found there :
https://stackoverflow.com/a/31838669/5186678