1

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());
viveksinghggits
  • 661
  • 14
  • 35

2 Answers2

2

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
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • yes, its working but the thing is the dateType should be java.sql.Date so that I can set it to a preparedstatement using setDate. setDate expects java.sql.Date – viveksinghggits Mar 23 '16 at 16:44
  • `java.sql.Date` has the format `YYYY-MM-DD` , there's no time part you can have with it – Ramanlfc Mar 23 '16 at 16:46
0

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());
user2004685
  • 9,548
  • 5
  • 37
  • 54