The Wayback Machine - https://web.archive.org/web/20231211060058/https://www.geeksforgeeks.org/how-to-convert-java-sql-date-to-java-util-date-in-java/
Open In App
Related Articles

How to Convert java.sql.Date to java.util.Date in Java?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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




// 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 milliseconds
        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
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 04 Sep, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials