DatePicker subtracts 1 day to date!

1

My problem is that in my program I load a date for a record through a datepicker, that date I keep in the database, all as Date type. Then when I call the list of the records stored in the database and show it on a table view the date is modified and I have exactly 1 day left of all the loaded dates ... The truth is that I can not think of if it can be a problem in some library or the mySQL driver, I need help. My database manager is phpMyAdmin, I'm using JavaFx in Netbeans 8.2.

Here I put my classes:

Trip Class (which is the type that is stored in the BD.

public class Viaje {

private IntegerProperty idViaje;
private Date fecha;
private StringProperty origen;
private StringProperty destino;
private StringProperty tipoNota;
private StringProperty nota;
private IntegerProperty cantidadKm;
private IntegerProperty dniChofer;
private StringProperty idAuto;
private StringProperty horaSalida;
private StringProperty horaRegreso;
private FloatProperty importe;

public Viaje() {

    this.idViaje = new SimpleIntegerProperty();
    this.fecha = fecha;
    this.origen = new SimpleStringProperty("");
    this.destino = new SimpleStringProperty("");
    this.tipoNota = new SimpleStringProperty("");
    this.nota = new SimpleStringProperty("");
    this.cantidadKm = new SimpleIntegerProperty();
    this.dniChofer = new SimpleIntegerProperty();
    this.idAuto = new SimpleStringProperty("");
    this.horaSalida = new SimpleStringProperty("");
    this.horaRegreso = new SimpleStringProperty();
    this.importe = new SimpleFloatProperty();


}

public Viaje(Integer idViaje,Date fecha,String origen,String destino, String tipoNota,String nota,Integer cantidadKm,Integer dniChofer,String idAuto,String horaSalida,String horaRegreso, Float importe){
    this.idViaje = new SimpleIntegerProperty(idViaje);
    this.fecha = fecha;
    this.origen = new SimpleStringProperty(origen);
    this.destino = new SimpleStringProperty(destino);
    this.tipoNota = new SimpleStringProperty(tipoNota);
    this.nota = new SimpleStringProperty(nota);
    this.cantidadKm = new SimpleIntegerProperty(cantidadKm);
    this.dniChofer = new SimpleIntegerProperty(dniChofer);
    this.idAuto = new SimpleStringProperty(idAuto);
    this.horaSalida = new SimpleStringProperty(horaSalida);
    this.horaRegreso = new SimpleStringProperty(horaRegreso);
    this.importe = new SimpleFloatProperty(importe);
}

public int getIdViaje() {
    return idViaje.get();
}

public void setIdViaje(int idViaje) {
    this.idViaje.set(idViaje);
}

public IntegerProperty idViajeProperty() {
    return idViaje;
}

public Date getFecha() {
    return fecha;
}

public void setFecha(Date fecha) {
    this.fecha = fecha;
}

public String getOrigen() {
    return origen.get();
}

public void setOrigen(String origen) {
    this.origen.set(origen);
}

public StringProperty origenProperty() {
    return origen;
}

public String getDestino() {
    return destino.get();
}

public void setDestino(String destino) {
    this.destino.set(destino);
}

public StringProperty destinoProperty() {
    return destino;
}

public String getTipoNota() {
    return tipoNota.get();
}

public void setTipoNota(String tipoNota) {
    this.tipoNota.set(tipoNota);
}

public StringProperty tipoNotaProperty() {
    return tipoNota;
}

public String getNota() {
    return nota.get();
}

public void setNota(String nota) {
    this.nota.set(nota);
}

public StringProperty notaProperty() {
    return nota;
}

public int getCantidadKm() {
    return cantidadKm.get();
}

public void setCantidadKm(int cantidadKm) {
    this.cantidadKm.set(cantidadKm);
}

public IntegerProperty cantidadKmProperty() {
    return cantidadKm;
}

public int getDniChofer() {
    return dniChofer.get();
}

public void setDniChofer(int dniChofer) {
    this.dniChofer.set(dniChofer);
}

public IntegerProperty dniChoferProperty() {
    return dniChofer;
}

public String getIdAuto() {
    return idAuto.get();
}

public void setIdAuto(String idAuto) {
    this.idAuto.set(idAuto);
}

public StringProperty idAutoProperty() {
    return idAuto;
}

public String getHoraSalida() {
    return horaSalida.get();
}

public void setHoraSalida(String horaSalida) {
    this.horaSalida.set(horaSalida);
}

public StringProperty horaSalidaProperty() {
    return horaSalida;
}

public String getHoraRegreso() {
    return horaRegreso.get();
}

public void setHoraRegreso(String HoraRegreso) {
    this.horaRegreso.set(HoraRegreso);
}

public StringProperty HoraRegresoProperty() {
    return horaRegreso;
}

 public float getImporte() {
    return importe.get();
}

public void setImporte(float Importe) {
    this.importe.set(Importe);
}

public FloatProperty ImporteProperty() {
    return importe;
}

Controller of the Main screen where I register and keep it in the database:

public class PrincipalController extends ControladorBaseDatosA {

@FXML
public void apretarRegistrarViaje(ActionEvent event) {

    ControladorViaje controladorV = new ControladorViaje();

    ControladorBaseDatosA id = new ControladorBaseDatosA();

    if (siEsInvalidoPrincipal()) {
        Viaje v1 = new Viaje();

        //Obtengo el metodo de id autoincrementable
        v1.setIdViaje(id.idViajeAutoIncrementable());
        v1.setOrigen(tOrigen.getText());
        v1.setDestino(tDestino.getText());
        v1.setTipoNota(tTipoNota.getText());
        v1.setNota(tNota.getText());
        v1.setCantidadKm(Integer.parseInt(tCantidadKm.getText()));
        v1.setIdAuto(tIdVehiculo.getText());
        v1.setDniChofer(Integer.parseInt(tDniChofer.getText()));
        //Obtengo la fecha del date picker
        v1.setFecha(java.sql.Date.valueOf(tFecha.getValue()));
        //Aca guardo los valores de hora 
        v1.setHoraSalida((sHoraSalida.getText()));
        v1.setHoraRegreso((sHoraRegreso.getText()));
        v1.setImporte(Float.parseFloat(sImporte.getText()));

        clickRegistrarViaje = true;
        JOptionPane.showMessageDialog(null, "Viaje registrado con exito");
        controladorV.insertarSQL(v1);
        limpiar();

    } else {
        if (tOrigen.getText().equals("") || tDestino.equals("") || tTipoNota.equals("") || tNota.equals("") || tCantidadKm.equals("") || tIdVehiculo.equals("") || tDniChofer.equals("") || sImporte.equals("")) {
            JOptionPane.showMessageDialog(null, "Ingrese algun dato");
            System.out.println("Ingresa algo vivo!");
        }
    }
}

Method used to reach the tableview fields:

public void llenarViajes(Connection connection, ObservableList<Viaje> listaViaje) {
    try {
      Statement ps = connection.createStatement();
      ResultSet rs = ps.executeQuery("SELECT * FROM viaje");
        while (rs.next()) {
            listaViaje.add(new Viaje(rs.getInt("idViaje"), rs.getDate("fecha"), rs.getString("origen"), 
                rs.getString("destino"),rs.getString("tipoNota"), rs.getString("nota"), rs.getInt("cantidadKM"), 
                rs.getInt("dniChofer"),rs.getString("idAuto"),rs.getString("horaSalida"), rs.getString("horaRegreso"),
                rs.getFloat("importe")));
        }  
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e, "Error: " + e.getMessage(), JOptionPane.ERROR_MESSAGE);
    }
}

Class controller of the window where the tableview is located, which is filled with the data stored in the database:

public class AdminViajesController extends ControladorBaseDatosA implements Initializable {

@FXML
private Button atras;
@FXML
private TableView<Viaje> lista;
@FXML
private TableColumn<Viaje, String> idColumna;
@FXML
private TableColumn<Viaje, Date> fechaColumna;
@FXML
private TableColumn<Viaje, String> origenColumna;
@FXML
private TableColumn<Viaje, String> destinoColumna;
@FXML
private TableColumn<Viaje, String> tipoNotaColumna;
@FXML
private TableColumn<Viaje, String> notaColumna;
@FXML
private TableColumn<Viaje, Integer> kmColumna;
@FXML
private TableColumn<Viaje, Integer> dniChoferColumna;
@FXML
private TableColumn<Viaje, String> idAutoColumna;
@FXML
private TableColumn<Viaje, String> hSalidaColumna;
@FXML
private TableColumn<Viaje, String> hRegresoColumna;
@FXML
private TableColumn<Viaje, Float> importe;

private ObservableList<Viaje> listaViaje;



@Override
public void initialize(URL location, ResourceBundle resouces){
    listaViaje = FXCollections.observableArrayList();
    ControladorBaseDatosA db = new ControladorBaseDatosA();
    db.llenarViajes(db.getConexion(), listaViaje);
    lista.setItems(listaViaje);

    idColumna.setCellValueFactory(new PropertyValueFactory<>("idViaje"));
    fechaColumna.setCellValueFactory(new PropertyValueFactory<>("fecha"));
    origenColumna.setCellValueFactory(new PropertyValueFactory<>("origen"));
    destinoColumna.setCellValueFactory(new PropertyValueFactory<>("destino"));
    tipoNotaColumna.setCellValueFactory(new PropertyValueFactory<>("tipoNota"));
    notaColumna.setCellValueFactory(new PropertyValueFactory<>("nota"));
    kmColumna.setCellValueFactory(new PropertyValueFactory<>("cantidadKm"));
    dniChoferColumna.setCellValueFactory(new PropertyValueFactory<>("dniChofer"));
    idAutoColumna.setCellValueFactory(new PropertyValueFactory<>("idAuto"));
    hSalidaColumna.setCellValueFactory(new PropertyValueFactory<>("horaSalida"));
    hRegresoColumna.setCellValueFactory(new PropertyValueFactory<>("horaRegreso"));
    importe.setCellValueFactory(new PropertyValueFactory<>("importe"));

}

I forgot to mention that at the time of loading the data, the date keeps it correctly in the database, that is, if I enter for example 20-11-2018 it is the date that appears in the database, but that when the tableview is full, I alter the date by subtracting 1 day from all of them.

I'll show you a couple of photos:

    
asked by N30 20.11.2018 в 14:11
source

3 answers

0

Something similar happened to me some time ago, and it turned out to be a problem with the TimeZone configured in PHPMyAdmin.

PHPMyAdmin has a TimeZone configuration for each user, so if you are consulting a date in a TimeZone different from PHPMyAdmin, you will apply the difference of hours to the date shown.

To make sure that the date that is recorded in the BD is the date shown by PHPMyAdmin, you should directly consult the BD with a BD manager other than PHPMyAdmin.

Probably what is happening is that you enter a line and record the date correctly in the BD, but when you show it in PHPMyAdmin you modify it to your TimeZone, and in the program you get the actual date of the BD.

    
answered by 20.11.2018 в 16:38
0

Try with

rs.getObject("fecha", LocalDate.class)

Review this answer, maybe that's where the solution lies.

    
answered by 20.11.2018 в 16:44
0

This same thing happened to me a few days ago in JavaScript. The problem is that the datepicker picks up the date in local format, while the database is saving the dates in UTC format. What you should do is read the date in the database and convert it to your local format.

I leave the following link where it shows examples to convert them in both directions:

Convert Date to LocalDate or LocalDateTime and Back

I hope it helps you.

    
answered by 20.11.2018 в 17:40