How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object.
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 accept any parameter.
Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.
Exception: The function does not throw any exception.
Java
import java.sql.*;
import java.text.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
long millis = System.currentTimeMillis();
java.sql.Date sqlDate = new java.sql.Date(millis);
java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
final String stringDate = dateFormat.format(utilDate);
System.out.println( "utilDate:" + stringDate);
System.out.println( "sqlDate:" + sqlDate);
}
}
|
Output:
utilDate:2021-01-20
sqlDate:2021-01-20
Last Updated :
04 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...