How to convert java.util.Date to java.sql.Date keeping hours, minutes and seconds?

3

I'm doing a Java program. In it I get a date of type java.util.Date , but I need it in java.sql.Date so I can insert it into the database. But on that date, I have hour, minute and second and I would like to insert with all this data. Does anyone know how to do this?

Thank you.

    
asked by anonymous 03.11.2017 / 21:24

1 answer

2

To convert from java.util.Date to java.sql.Date :

java.util.Date a = ...;
java.sql.Date b = new java.sql.Date(a.getTime());

To insert date and time, use class java.sql.Timestamp :

java.util.Date a = ...;
java.sql.Timestamp b = new java.sql.Timestamp(a.getTime());

You can use Timestamp with a PreparedStatement in method setTimestamp(int, Timestamp) ". To get one of these ResultSet , use the method getTimestamp(int) or the method getTimestamp(String) .

    
03.11.2017 / 21:30