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,