top of page

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

Updated: Mar 23, 2023

java.util.Date and java.sql.Date are both Java classes that represent dates and times. However, there are some key differences between them in terms of their functionality and usage.


The java.util.Date class is a part of the Java SE API and is used to represent a specific point in time. It stores the date and time information as a long integer, which represents the number of milliseconds since January 1, 1970, 00:00:00 GMT (also known as the Unix epoch). This class provides various methods to manipulate and retrieve the date and time information such as getTime(), setTime(), toString(), and more. However, the java.util.Date class is not very efficient for performing date and time arithmetic, formatting, or parsing, and its methods have been deprecated since Java 1.1.


The java.sql.Date class is a part of the Java Database Connectivity (JDBC) API and is used to represent a date value in a SQL database. It extends the java.util.Date class, but only stores the date portion of the date-time value. It does not store the time information, and the time is set to 00:00:00. This class provides various constructors to create a new instance of java.sql.Date, such as Date(long millis) and Date(int year, int month, int day). It also provides various methods to manipulate and retrieve the date information, such as valueOf(String s), toLocalDate(), and more.

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

Here's an example Java program that demonstrates how to convert a java.util.Date object to a java.sql.Date object:

import java.util.Date; 
import java.sql.*;  

public class DateConverter 
    public static void main(String[] args) 
        // create a java.util.Date object
        Date utilDate = new Date();       
        System.out.println("java.util.Date: " + utilDate);        
        // convert java.util.Date to java.sql.Date       
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());       
        System.out.println("java.sql.Date: " + sqlDate);    

In this program, we first create a java.util.Date object using the new operator. Then, we use the getTime() method to get the time value of the utilDate object in milliseconds since January 1, 1970, 00:00:00 GMT. Finally, we pass this value to the constructor of java.sql.Date to create a new java.sql.Date object.



java.util.Date is a general-purpose class for representing dates and times, while java.sql.Date is a specialized class for working with dates in SQL databases. If you are working with SQL databases, it is recommended to use java.sql.Date instead of java.util.Date to avoid potential data loss or conversion issues. If you are working with dates and times outside of a database context, you may prefer to use the java.time package introduced in Java 8, which provides a more comprehensive set of classes for working with dates and times.


Recent Posts

See All


bottom of page