Date problem in spring boot + mysql

Asked

Viewed 322 times

-1

Good afternoon, I’m trying to make a findAll on spring boot because the date is coming with 1 day late.

public List<ContaReceber> findAll() {
    return contaReceberRepository.findAll();
}

jdbc:mysql://localhost:3306/meubanco?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=America/Sao_Paulo

I registered an account to receive day 08-11-2019, the data is being saved correctly, when making a findAll, the result returns 07-11-2019.

2 answers

1


Hello, put this on your springApplication:

@PostConstruct
  public void init(){
    // Setting Spring Boot SetTimeZone
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
  }

0

Hello

In your case, the reason you are returning the date 1 day apart is that your database is configured with the UTC-3 date which is America/Sao_Paulo, that you added to the database connection:

jdbc:mysql://localhost:3306/meubanco?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=America/Sao_Paulo

Although your bank is with the UTC-3 that is correct, your application is not, and ends up taking the local UTC, which is UTC-3, but when redeeming the data it decreases the -3 hours which makes the date decrease 1 day, because, your application thinks that the date returned is in UTC format, then reduces -3 hours.

So the best solution is for the application and the database to be configured with the same zone, as Melchior Felix said, but instead of "UTC", "America/Sao_paulo".

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @PostConstruct
    public void init() {
        TimeZone.setDefault(TimeZone.getTimeZone("America/Sao_Paulo"));
    }
}

Browser other questions tagged

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