How to convert java.sql.Date into java.util.Date and
vice-versa is a popular JDBC
interview question which is also asked as follow-up question of difference
between java.sql.Date and java.util.Date which we have saw in our last
article. Since both SQL date and Util date stores values as long millisecond ,
its easy to convert them back and forth. Both java.sql.Date and java.util.Date provides
convenient method called getTime() which returns long millisecond
equivalent of wrapped date value. Here is quick example of converting java.util.Date to java.sql.Date and then
back to util Date. This article is in next of earlier post on Java Date and
Time API like How
to convert Date to String and How
to 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
As stated above we have used getTime() method in
this example to transfer long millisecond value form one Date type, java.sql.Date to another
e.g. java.util.Date, in order to convert SQL Date into util Date:
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
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 java.util.Date in Java.
as you have seen its pretty easy to convert them by using getTime() method.
There is another way to convert them using 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

No comments:
Post a Comment