Difference between java.util.Date and java.sql.Date
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Date; class Main { public static void main(String[] args) { // Create a java.util.Date object Date date = new Date(); // Print the date System.out.println("java.util.Date: " + date); // Wed Oct 27 15:24:35 GMT 2021 // Get the millisecond of the date System.out.println("Millisecond: " + date.getTime()); // 1635343475000 } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.sql.Date; class Main { public static void main(String[] args) { // Create a java.sql.Date object Date date = new Date(System.currentTimeMillis()); // Print the date System.out.println("java.sql.Date: " + date); // 2021-10-27 // Try to get the millisecond of the date System.out.println("Millisecond: " + date.getTime()); // 1635292800000 } } |
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.Daterepresents a specific instant in time in a Java application, with millisecond precision and without timezone. Ajava.util.Dateobject represents both date and time information, whilejava.sql.Dateobject only stores the date information without any time information. - The
java.sql.Datemaps to the SQLDATEdatatype, which doesn’t store time information. This is why we needjava.sql.Datein addition tojava.util.Dateas SQLDATEdatatype doesn’t store hour, minute, and second information. - To store a
java.util.Dateobject in database as SQLDATEdatatype, a conversion tojava.sql.Dateobject is required. Similarly, an SQLDATEvalue is retrieved asjava.sql.Dateobject from the database, and a conversion tojava.util.Dateis needed to use it in a Java application. - The
toString()ofjava.util.Datereturns a string representation of this date inmon dd hh:mm:ss zzz yyyyformat whiletoString()method ofjava.sql.Dateformats a date inyyyy-mm-ddformat. - The
java.sql.Dateis a subclass ofjava.util.Date, so it inherits all public methods ofjava.util.Datesuch asgetHours(), getMinutes(), getSeconds(), and correspondingset*()methods. Thejava.sql.Dateactually overrides all the time operations inherited fromjava.util.Date, but all these methods throw ajava.lang.IllegalArgumentExceptionsince 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.Datenorjava.sql.Date, but useZonedDateTimeinstead. 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.Datenorjava.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:
That’s all about the differences between java.util.Date and java.sql.Date.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)