Tableview bring date, mysql database

Asked

Viewed 230 times

2

I have a Table View, which receives the data of a table in my Database, until then I was able to bring the data normally, but the date is different from the one registered in the database, always behind 1 day before.

For example:

In the bank: 2018-09-06 In my Tableview returns: 2018-09-05

What could be causing this mistake?

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”));
  • When you print out the dates they are correct?

  • In the bank are correct, when I show in my tableview always comes with 1 less day, another curiosity, I have 1 filter per date, when filter by today’s date for example, 13/09/2018, it brings me the corresponding date that is 12/09/2018.

  • I believe there is a problem between java.sql.Date (the one coming from the database) and java.util.Date, it is not necessarily a problem in Tableview because it only displays the saved information.

  • What do you tell me to do to solve this problem? I’m not sure what to do

1 answer

1


I believe it is a problem when converting dates. I will suggest that you use the new Java 8 Date API Localdate/Localdatetime. 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 Column table:

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 see this article: https://code.makery.ch/blog/javafx-8-tableview-cell-renderer/

Note: Also check if there are no time zone problems, in which case use Zonedlocaldate and beware of null.

  • Thank you very much, I had to use the plusDays, with it worked properly, thanks for the attention, helped me a lot.

Browser other questions tagged

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