Convert java.util.date to java.sql.Date

3

With this line of code will I be able to pass java.util.Date to java.sql.Date ?

java.sql.Date sqlDate = java.sql.Date.valueOf(String.valueOf(date));

Given that you date this format:

Sat Jul 01 00:00:00 GMT+01:00 2017
    
asked by anonymous 13.07.2017 / 17:35

1 answer

1

If the variable date is a type java.util.Date , it only suffices:

java.sql.Date sqlDate = new java.sql.Date(date.getTime());

Now if you are converting a string date to sql.Date , it will only be possible if it is in the yyyy-[m]m-[d]d format, where the month and day can contain only one digit if the second is zero, as in 2017-05-01 which can be passed as 2017-5-1 .

However, if possible, I recommend using Joda Time that makes operations with dates and time periods more flexible .

Reference:

13.07.2017 / 17:41