1

I have a code in Spring with java 8 to insert the current date and time in my Mysql 5.7 database, the problem is that when I check the database it adds one more hour to the one that corresponds, the data type in the database is Datetime.

I already checked the time_zone in Mysql with SELECT @@ time_zone and it shows me: + ------------- + | @@ time_zone | + ------------- + | SYSTEM | + ------------- +

and also select now (); and it shows me the current time correctly.

My code is the following:

The class where I implement it:

LocalDateTime actual = LocalDateTime.now();
DTO.getPer().setDateUpdate(actual);

In the model:

@Column(name = "date_update")
private LocalDateTime dateUpdate;

Thank you.

Jhon
  • 17
  • 1
  • 7

1 Answers1

0

This is definetly connected with TimeZones. You have UTC+1 time zone. LocalDateTime does not contain this informatin. Do use ZonedDateTime instead.

@Column(name = "date_update", columnDefinition="TIMESTAMP")
private ZonedDateTime dateUpdate;

How to map a TIMESTAMP column to a ZonedDateTime JPA entity property?

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Thanks for the answer, it still doesn't work, I have tested the application on another computer and it works fine, I have checked Mysql and Java time and it shows the correct time, I am currently working on Mac OSX Catalina. – Jhon Aug 19 '20 at 00:58
  • @Jhon I think you use `ZonedDateTime` incorrectly. Could you provide a test case? Probably with in-memory DB? – Oleg Cherednik Aug 19 '20 at 00:59