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());
4
  • 1
    Possible duplicate of How to convert java.util.date to java.sql.date? Commented Mar 23, 2016 at 16:37
  • The toString() method of sql.Date only returns the date part. Use SimpleDateFormat to format it in the format you want. Commented Mar 23, 2016 at 16:55
  • I think this is a duplicate, you can find the answer here: stackoverflow.com/questions/530012/… Commented Jun 28, 2017 at 18:28
  • thanks @Scott123180 , i got it though Commented Jun 28, 2017 at 18:36

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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
java.sql.Date has the format YYYY-MM-DD , there's no time part you can have with it
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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.