This post will discuss differences between the java.util.Date and java.sql.Date in Java.

1. Overview of java.util.Date

java.util.Date is a class that represents a specific instant in time in a Java application, with millisecond precision and without timezone. A java.util.Date object contains both date and time information, such as year, month, day, hour, minute, second, and millisecond. This information can be accessed and modified using various methods provided by the class. It is mutable, which means that it can be changed after it is created. Let’s see an example of how to create and use a java.util.Date in Java:

Download  Run Code

2. Overview of java.sql.Date

java.sql.Date is a class that extends java.util.Date and implements the java.sql.SQLData interface. It is used by JDBC to identify an SQL DATE type. An SQL DATE type only stores year, month, and day information without any time information. A java.sql.Date object only contains date information without any time information. Let’s see an example of how to create and use a java.sql.Date in Java:

Download  Run Code

3. Difference between java.util.Date and java.sql.Date

The functionality of java.util.Date and java.sql.Date are similar. They both implement java.io.Serializable. However, they also have some differences and trade-offs that we must understand:

  • The java.util.Date represents a specific instant in time in a Java application, with millisecond precision and without timezone. A java.util.Date object represents both date and time information, while java.sql.Date object only stores the date information without any time information.
  • The java.sql.Date maps to the SQL DATE datatype, which doesn’t store time information. This is why we need java.sql.Date in addition to java.util.Date as SQL DATE datatype doesn’t store hour, minute, and second information.
  • To store a java.util.Date object in database as SQL DATE datatype, a conversion to java.sql.Date object is required. Similarly, an SQL DATE value is retrieved as java.sql.Date object from the database, and a conversion to java.util.Date is needed to use it in a Java application.
  • The toString() of java.util.Date returns a string representation of this date in mon dd hh:mm:ss zzz yyyy format while toString() method of java.sql.Date formats a date in yyyy-mm-dd format.
  • The java.sql.Date is a subclass of java.util.Date, so it inherits all public methods of java.util.Date such as getHours(), getMinutes(), getSeconds(), and corresponding set*() methods. The java.sql.Date actually overrides all the time operations inherited from java.util.Date, but all these methods throw a java.lang.IllegalArgumentException since SQL Date values does not have a time component.

Normally, a subclass usually extends a parent class’s functionality, but here java.sql.Date subclass is actually restricting features available in the parent java.util.Date class.

4. What to use and when?

As we have seen, java.util.Date and java.sql.Date are both useful classes to store and process date information in Java. Here are some general recommendations on how to choose between them:

  • If you need to store a specific instant in time in a Java application, with millisecond precision and without timezone, we can use java.util.Date.
  • If you need a class to interact with databases that support the SQL DATE type, which only stores year, month, and day information without any time information, we should use java.sql.Date.
  • If you need to store a specific instant in time in a Java application, with millisecond precision and with timezone, we should use neither java.util.Date nor java.sql.Date, but use ZonedDateTime instead. This class will provide the advanced functionality and performance for single-threaded or multithreaded applications.
  • If you need to store a specific instant in time in a Java application, with millisecond precision and without timezone, but also support multithreading, we should use neither java.util.Date nor java.sql.Date, but use Instant instead. This class will ensure that the date is immutable and thread-safe and prevent any data inconsistency or concurrency issues.

 
Also See:

Convert java.util.Date to java.sql.Date

That’s all about the differences between java.util.Date and java.sql.Date.