TableView bring date, from the mysql database

2

I have a Table View, which receives the data from a table in my Bank, until then I was able to bring the data normally, but the date is coming different from the one registered at the bank,

For example:

The bank is: 2018-09-06 In my TableView returns: 2018-09-05

What could be causing this error?

Some data on how I am declaring the date:

import java.util.Date;

private Date datavisita;

public Date getDatavisita() {
return datavisita;
}

public void setDatavisita(Date datavisita) {
this.datavisita = datavisita;
}

//metodo para filtrar a data:

v.setDatavisita(rs.getDate(“datavisita”));

//declaração da coluna data
@FXML
TableColumn<VW_Visitas_Sonho, Date> colData;

// configurar as colunas de acordo com o objeto VW_Visitas_Sonho

colData.setCellValueFactory(new PropertyValueFactory<>(“datavisita”));
    
asked by anonymous 13.09.2018 / 20:02

1 answer

1

I think it's a problem when converting dates. I'll suggest using the new Java 8 datetime API LocalDate / LocalDateTime . The java.sql.Date comes with a method called toLocalDate () that you can use.

private LocalDate datavisita;

public Date getDatavisita() {
    return datavisita;
}

public void setDatavisita(LocalDate datavisita) {
    this.datavisita = datavisita;
}

Here we will use the toLocalDate () method.

System.out.println(rs.getDate(“datavisita”).toLocalDate()); // Verifique antes
v.setDatavisita(rs.getDate(“datavisita”).toLocalDate());

Change the declaration of your table Column:

TableColumn<VW_Visitas_Sonho, LocalDate> colData;

// [...] A configuração está correta
colDate = new TableColumn<>("Data");
colData.setCellValueFactory(new PropertyValueFactory<>(“datavisita”));

If the problem persists for some reason you can easily add a day to LocalDate using the plusDays () method

v.setDatavisita(rs.getDate(“datavisita”).toLocalDate().plusDays(1));

If you have problems formatting the date, please see this article: link

Note: Also check if there are no time zone problems, in this case use ZonedLocalDate and watch out for null.

    
14.09.2018 / 16:58