Sunday, July 18, 2021

JDBC - How to Convert java.sql.Date to java.util.Date in Java with Example

How to convert java.sql.Date into a java.util.Date and vice-versa is a popular JDBC interview question which is also asked a follow-up question of the difference between java.sql.Date and java.util.The date which we have seen in our last article. Since both SQL date and Util date store values as a long millisecond, converting them back and forth is easy. Both java.sql.Date and java.util.The date provides a convenient method called getTime() which returns a long millisecond equivalent of a wrapped date value. Here is a quick example of converting java.util.Date to java.sql.Date and then back to util Date. 

This article is in the next of earlier posts on Java Date and Time API  like How to convert Date to String and find current Date and Timestamp in Java on any timezone. If you haven't read them already, you may find them useful.



Java Example to convert java.sql.Date to java.util.Date

How to convert java.sql.Date to java.util.Date in JavaAs stated above, we have used the getTime() method in this example to transfer long millisecond values from one Date type, java.sql.Date to another like the java.util.Date, to convert SQL Date into util Date. You can further see these free JDBC courses to learn more about the Date and time data types in JDBC and Java. 





public class DateConverter {

    public static void main(String args[]) throws InterruptedException {
     
        //creating instances of java.util.Date which represents today's date and time
        java.util.Date now = new java.util.Date();
        System.out.println("Value of java.util.Date : " + now);
     
        //converting java.util.Date to java.sql.Date in Java
        java.sql.Date sqlDate = new java.sql.Date(now.getTime());
        System.out.println("Converted value of java.sql.Date : " + sqlDate);
     
        //converting java.sql.Date to java.util.Date back
        java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
        System.out.println("Converted value of java.util.Date : " + utilDate);
    }
 
}

Output:
Value of java.util.Date : Tue Apr 10 00:12:18 VET 2012
Converted value of java.sql.Date : 2012-04-10
Converted value of java.util.Date : Tue Apr 10 00:12:18 VET 2012


That's all on how to convert java.sql.Date into a java.util.Date in Java. As you have seen, it's pretty easy to convert them by using the getTime() method. There is another way to convert them using the String value of one Date and then converting String to java.util.Date but that's not recommended, and using getTime() is more convenient than that.


Other JDBC tutorials for Java programmer

5 comments:

  1. when we converted java.util.date to java.sql.date the output is 2012-04-10, we are not getting the time here. What if I want the time also.

    ReplyDelete
  2. Hi,
    i am facing same problem, what vivek is facing..,any solution for this?

    ReplyDelete
    Replies
    1. Hello Shantha and Vivek, the java.sql.Date only captures date part, If you need both date and time then please use java.sql.Timestamp class instead of java.sql.Date.

      Delete
  3. what is this ??? i want more exameples and solutions

    ReplyDelete
    Replies
    1. Hi, what is your doubt? If you can tell, I may be able to help

      Delete

Feel free to comment, ask questions if you have any doubt.