Reputation: 680
So I am trying to convert the java.util.date
(Wed Mar 23 21:58:14 IST 2016) to java.sql.date. The output I am having by .getTime() is 2016-03-23. I want it to be in the form date time or so.
I am not getting the time in this. I want time also in the converted date.
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Upvotes: 1
Views: 3673
Reputation: 9658
As per the JavaDoc of java.sql.Date
, it has only Date and not Time.
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.
To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
If you want the time also along with the date then you should be looking at java.sql.Timestamp
instead.
Here is the code snippet:
java.sql.Timestamp sqlDateTime = new java.sql.Timestamp(utilDate.getTime());
Upvotes: 0
Reputation: 8354
java.sql.Date
is equivalent of sql Date type , i think you are looking for java.sql.Timestamp
Date d = new Date();
Timestamp stamp = new Timestamp(d.getTime());
System.out.println(stamp); //2016-03-23 22:08:39.686
Upvotes: 2