Write a Java Program to Convert java.util.Date into java.sql.Date


The code demonstrates how to convert a java.util.Date object to a java.sql.Date object. First, a new java.util.Date object is created using the default constructor, which creates a Date object with the current date and time.

Then, a new java.sql.Date object is created by passing the time value of the java.util.Date object to the constructor of java.sql.Date. Next, a DateFormat object is created with a specific date format of "dd-MM-yyyy". This is used to format the java.util.Date object into a string with the specified format. Finally, the formatted string and the java.sql.Date object are printed to the console.

Source Code

import java.util.*;
import java.sql.*;
import java.text.*;
public class Convert_Date
{
	public static void main(String[] args)
	{
		java.util.Date util_dt = new java.util.Date();
		java.sql.Date sql_dt = new java.sql.Date(util_dt.getTime());
 
		DateFormat dtFormat = new SimpleDateFormat("dd-MM-yyyy");
		final String str_dt = dtFormat.format(util_dt);
 
		System.out.println("Util Date : " + str_dt);
		System.out.println("SQL Date  : " + sql_dt);
	}
}

Output

Util Date : 17-12-2022
SQL Date  : 2022-12-17

Example Programs