How to convert java.util.Date to java.sql.Date?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Converting java.util.Date to java.sql.Date is a common requirement in Java applications, especially when there is a need to interact with databases that store dates. Java, being a versatile programming language, supports this conversion through its well-defined APIs. The need for conversion arises because java.util.Date includes both date and time information whereas java.sql.Date is concerned solely with the date component. Here, we will look at how to perform this conversion, explain why it is necessary, and discuss some related considerations.
Understanding the Java Date Types
java.util.Date: This class represents a specific instant in time, with millisecond precision. It includes both date and time information.
java.sql.Date: This class extends java.util.Date, adding additional features to interact with SQL databases. Notably, java.sql.Date only concerns itself with the date component (year, month, day) and zeros out the hours, minutes, seconds, and milliseconds.
Conversion Process
Converting java.util.Date to java.sql.Date involves:
- Creating an instance of
java.util.Date. - Extracting the time value in milliseconds.
- Constructing a
java.sql.Dateobject using the milliseconds.
Here’s a simple code example that demonstrates this conversion:
In the above example:
utilDate.getTime()method is used to get the time in milliseconds from the Java epoch (January 1, 1970, 00:00:00 GMT).- This millisecond value is then passed to the constructor of
java.sql.Date.
Key Considerations
When dealing with these conversions, there are several considerations to keep in mind:
- Time Component: As noted,
java.sql.Datedisregards the time component. This is important for database operations where only the date part is needed. If the time component is important, consider usingjava.sql.Timestampinstead. - Database Compatibility: Always check whether the database column you intend to populate expects a date or a datetime/timestamp. Misalignments here can cause unintended data truncation or conversion errors.
- Time Zones: Handling dates often involves managing time zones. Both
java.util.Dateandjava.sql.Dateoperate in the context of the default system time zone, unless managed explicitly in the code or database.
Practical Applications
Understanding how to convert between these two date types is particularly useful when:
- Interfacing with JDBC: When you're fetching or inserting date values from/to a SQL database.
- Separating Concerns: When the application logic uses
java.util.Datebut the database layer requiresjava.sql.Date. - Legacy Code Maintenance: Older Java codebases often use
java.util.Date, and interoperability with newer frameworks or libraries might require conversion.
Summary Table
The following table summarizes the key aspects and differences:
| Feature | java.util.Date | java.sql.Date |
| Includes Time | Yes | No |
| Precision | Millisecond | Day |
| Usage | General-purpose timekeeping | Database (date only) |
| Constructor Input | Milliseconds since epoch | Milliseconds since epoch |
Concluding Thoughts
The conversion from java.util.Date to java.sql.Date is straightforward but requires understanding the nuances and limitations of both classes. Proper handling of dates in Java is crucial for creating robust and error-free applications, especially when interacting with databases. Always test your date manipulations to account for time zones and ensure that the correct date information is being stored and retrieved.
Related reading
- How to convert result table to JSON array in MySQL
- How to convert SQL Query result to PANDAS Data Structure?
- How to convert string into timestamp in Presto Athena?
- how to convert string to numerical values in mongodb
- How to convert jsonString to JSONObject in Java
- How to convert List to Map?
- How to convert timestamp to datetime in MySQL?
- How to copy a row and insert in same table with a autoincrement field in MySQL?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.