Select returning dates on object other than what is registered in MySQL database

0

I'm having a problem when I'm writing the contents of a table and storing it in a list so the date is one day less.

I've done a lot of research on the web and I did not find a solution to the problem.

Below the registration method:

public String cadastrarPeriodo(LocalDate dataInicio, LocalDate dataFim) throws SQLException {
    String message = null;

    Connection con = ConexaoMySQL.getConexaoMySQL();
    PreparedStatement stmt = null;

    try {
        stmt = con.prepareStatement(inserirPeriodoBancoHora);
        stmt.setDate(1, Date.valueOf(dataInicio));
        stmt.setDate(2, Date.valueOf(dataFim));

        int res = stmt.executeUpdate();

        if(res == 1){
            message = "Dados Registrados com Sucesso!";
        } 
    } catch (Exception e) {
        e.printStackTrace();
        message = "Erro ao Salvar os Dados";
    } finally {
        stmt.close();
        con.close();
    }

    return message;
}

Search method

public List<PeriodoBancoHora> listarPeriodosCadastrados() throws SQLException {
    List<PeriodoBancoHora> listaPeriodoBancoHora = new ArrayList<PeriodoBancoHora>();

    Connection con = ConexaoMySQL.getConexaoMySQL();
    PreparedStatement stmt = null;

    try {
        stmt = con.prepareStatement(listarPeriodosCadastrados);

        ResultSet res = stmt.executeQuery();

        if(res.next()){
            do {
                PeriodoBancoHora periodoBancoHora = new PeriodoBancoHora();
                periodoBancoHora.setIdPeriodoBancoHora(res.getInt(1));
                periodoBancoHora.setDataInicio(res.getDate(2).toLocalDate());
                periodoBancoHora.setDataFim(res.getDate(3).toLocalDate());
                listaPeriodoBancoHora.add(periodoBancoHora);
            } while (res.next());
        } 
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stmt.close();
        con.close();
    }

    return listaPeriodoBancoHora;
}

In the database is registering the value that I am informing on the screen, but the value returned is subtracting 1 day, according to the images below:

The MySQL connector I'm using is mysql-connector-java-6.0.6.jar.

Thank you in advance.

Sincerely,

    
asked by anonymous 26.01.2018 / 22:00

2 answers

1

Thanks for the feedback, I was able to solve the problem by adding the tips I received here. First the indication of Krismorte, led me to the understanding that my problem was connected to timezone, I researched how to adjust timezone in the database, other ways to apply the suggested configuration, but nothing worked. After rLinhares indicated the update of the connection driver, I did it and the problem persisted, so I researched how to enter timezone directly into the connection string of the bank and to my surprise, there was already a timezone = UTC configuration that I changed for timezone = America \ Sao_Paulo.

And started to return the value correctly.

Thanks for the support of all and there are options for anyone facing similar problems.

    
29.01.2018 / 13:48
0

Replace this line:

res.getDate(2).toLocalDate()

by:

res.getDate(2).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

link

    
26.01.2018 / 22:10