I have a java.util.Date, which has a particular time (not the current one), and when I insert that Date in my database, it shows me the time 3 hours in advance, in the database I have a field DATETIME type that keeps the time, this is my code:
From here I capture the date and time that the user enters:
String[] tiempo = jTextField4.getText().split(":");
String hora = tiempo[0];
String minutos = tiempo[1];
Date fecha = jDateChooser1.getDate();//Obtenemos el Date
fecha.setHours(Integer.parseInt(hora));
fecha.setMinutes(Integer.parseInt(minutos));
Then from another method that asks me for a parameter I execute the SQL statement:
PreparedStatement ps = conn.prepareStatement("INSERT INTO reserva (nombre, dni, fecha, id_mesa, vigente) VALUES (?, ?, ?, ?, 1)", Statement.RETURN_GENERATED_KEYS);
ps.setString(1, reserva.getNombre());
ps.setLong(2, reserva.getDni());
ps.setObject(3, reserva.getFecha());
ps.setInt(4, reserva.getMesa().getId());
ps.execute();
ps.close();
When I print the Date by console, I get back the time perfectly, but when I insert it into the database, it gives me 3 hours in advance. That could be happening? Why do not you show me the time I want?
Greetings and thanks from now.