viveksinghggits
viveksinghggits

Reputation: 680

converting java.util.date to java.sql.date

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

Answers (2)

ninja.coder
ninja.coder

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

Ramanlfc
Ramanlfc

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

Related Questions