Date less day in Mysql

Asked

Viewed 440 times

-1

I’m having a little, boring problem that I can’t seem to solve.

I am making a java application using Spring Framework and using Vuejs as front. The database is Mysql.

The problem is that when I try to save a date equal to or earlier than 02/16/2019, the date is saved with 1 day less in Mysql. If it is a later date, the date is saved correctly.

Edit: I realized that in the year 2020 also occurs this problem, seems to be something related to this period from 01/01 to 16/02.

edit2: I noticed that the problem starts in November, from 05/11. That is, any date I try to save between 05/11 and 16/02, is saved in the bank with the previous day. If not in that period, the date is normally saved.

I have already confirmed that the date arrives correctly in java, so the problem is not in Vuejs. And I also found that before making the call to save the entity through Jparepository, the date is still correct, so I think it’s a problem in Mysql, or some configuration that needs to be done in Spring.

I already tweaked the Spring properties file settings using these properties:

  • spring.jpa.properties.hibernate.jdbc.time_zone=America/Sao_paulo
  • user.Timezone=America/Sao_paulo

But none of them solved the problem.

Can anyone help me out there? VLW!

  • 1

    Put the code snippet q saves the date in pfvr database.

  • I use the Jparepository save() method, and my entity has a Localdate type parameter and in Mysql the field is Date type. I don’t do any treatment before calling save() from Jparepository, I take the object the way it comes from the front and send it to Jparepository.

  • In this case it would be this: Return Repository.save( member ); Note: This object returned by Jparepository is with the correct date, but in the saved bank with the date -1

  • 1

    Do you use any Date notes in your class? As far as I know there are two possible ones, @Temporal(Temporaltype.DATE) and @Temporal(Temporaltype.TIMESTAMP). Take a look at this link and try to format it before going to the bank to see if it solves.https://www.baeldung.com/hibernate-date-time

  • This link you sent me says that the classes in the java.time. * package do not require annotation. I am using Localdate.

  • When I used the Ibernate I didn’t use the util. Date and everything went well. Would it be quiet for you to test with Calendar? @Temporal(value=Temporaltype.DATE) private Calendar dataNascimento;

  • If it is difficult for you to use Calendar, use Date with the above annotation to test.

  • I think I got it. I found these two parameters to put in the BD connection string: useTimezone=true&serverTimezone=America/Sao_paulo. And I also used properties user.Timezone=America/Sao_paulo in Spring properties.

Show 3 more comments

1 answer

0

Try these settings in the . properties file:

# MYSQL
spring.datasource.url= jdbc:mysql://localhost/MeuDataBase?useTimezone=true&serverTimezone=America/Sao_Paulo
spring.datasource.username= MeuLogin
spring.datasource.password= MinhaSenha

# JPA
spring.jpa.database-platform= org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql= true
spring.jpa.open-in-view= true
spring.jpa.database= mysql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.