2
I have a problem when I list the contents of a table and store in a list the date is one day less.
I’ve done a lot of research on the web and can’t find a solution to the problem.
Below the method of registration:
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 recording the value I am reporting on the screen, however the value returned is subtracting 1 day, as the images below:
The Mysql connector I’m using is mysql-Connector-java-6.0.6.jar.
Thanks in advance.
Sincerely yours,
I had a similar problem with SQL Server and just changed the drive. See how compatible the drive is with the version of Mysql you are using Or see also the Data Type being used in Java vs Date type of Mysql
– Krismorte
rLinhares, thanks for the feedback, updated the driver, but what solved the problem was a configuration on the connection. Mysql is pretty boring with some settings, to make the connection the first time I had to set Timezone in connectionfactory, and I picked up a UTC standard, was only change to America Sao_paulo that started working.
– Erick Menezes