Java : java.util.Date and java.sql.Date Conversion

java.sql.Date a thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE around. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT (epoch). java.sql.Date instance normalized by settings hours, minute, seconds and milliseconds to zero.

Note: java.sql.Date have only Date part no any time related values.



import java.util.Date;

public class DateSQLConversion {

public static void main(String[] args) {
java.util.Date utilDate = new java.util.Date();
System.out.println("Java Util Date : " + utilDate);

// contains only date information without time
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println("Java SQL Date : " + sqlDate); 

//convert sql date to java date
java.util.Date newUtilDate=new Date(sqlDate.getTime());
System.out.println("Java Util Date : " + newUtilDate);



Java Util Date : Mon Jul 30 11:06:20 PDT 2018
Java SQL Date : 2018-07-30
Java Util Date : Mon Jul 30 11:06:20 PDT 2018