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

If we've the Date item of the sq. bundle, then we will without difficulty convert it into a util Date object. We need to skip the getTime() technique whilst growing the util Date item.
 
java.util.Date  utilDate = new java.util.Date(sqlDate.getTime());

It will give us util Date object.

getTime() method

Syntax:

public long getTime()

Parameters: 

The function does not be given any parameter.
 
Return Value: 

Exception: The function does not throw any exception.

Java

// Java program to Convert java.sql.Date to java.util.Date

import java.sql.*;
import java.text.*;
import java.util.*;
public class GFG {

	public static void main(String[] args)
	{

		// sql date object takes time in mili seconds
		long millis = System.currentTimeMillis();

		// creating sql date object
		java.sql.Date sqlDate = new java.sql.Date(millis);

		// creating util date object by passing gettime()
		// method of sql date class
		java.util.Date utilDate = new java.util.Date(sqlDate.getTime());

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		// converting the util date into string format
		final String stringDate = dateFormat.format(utilDate);

		// printing both dates
		System.out.println("utilDate:" + stringDate);
		System.out.println("sqlDate:" + sqlDate);
	}
}

Output:

utilDate:2021-01-20
sqlDate:2021-01-20

 

Submit Your Programming Assignment Details