Date and Time

How to Convert java.util.Date to java.sql.Date

Table of Contents

Program

import java.util.Date;
public class DateToSql {

	public static void main(String[] args) {
		
		Date date = new Date();	
		System.out.println("Java Date & Time :" + date);
		long millis = System.currentTimeMillis();
		Date date1 = new java.sql.Date(millis);	
		System.out.println("Sql Date :" + date1);
	}

}

Output

Java Date & Time :Mon Apr 12 11:32:16 IST 2021
Sql Date :2021-04-12

Description

public class Date

extends Date
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.