The question and other answers seem to be over-thinking the issue.
A java.sql.Date is merely a java.util.Date with its time set to 00:00:00
.
Date-Only versus Date-Time
The core problem is:
- SQL
In SQL, the DATE
data type stores a date-only, without a time-of-day.
- JAVA
In the badly designed date-time library bundled with the early versions of Java, they failed to include a class to represent a date-only.
Instead of creating a date-only class, the Java team made a terrible hack. They took their date-time class (the misnamed java.util.Date
class, containing both date and time) and extended it to have an instance set its time-of-day to midnight UTC, 00:00:00
. That hack, that subclass of j.u.Date, is java.sql.Date
.
All this hacking, poor design, and misnaming has made a confusing mess.
Which To Use
So when to use which? Simple, after cutting through the confusion.
- When reading or writing to a database’s date-only column, use
java.sql.Date
as it clumsily tries to mask its time-of-day.
- Everywhere else in Java, where you need a time-of-day along with your date, use
java.util.Date
.
- When you have a java.sql.Date in hand but need a java.util.Date, simply pass the java.sql.Date. As a subclass, a java.sql.Date is a java.util.Date.
Even Better
In modern Java, you know have a choice of decent date-time libraries to supplant the old and notoriously troublesome java.util.Date, .Calendar, SimpleTextFormat, and java.sql.Date classes bundled with Java. The main choices are:
Both offer a LocalDate
class to represent a date only, with no time-of-day and no time zone.
Modernized JDBC
Hopefully JDBC will be updated to provide getter and setter methods for the java.time LocalDate.
setObject | getObject
This article published by Oracle suggests that indeed the JDBC in Java 8 has been updated transparently to map a SQL DATE
value to the new java.time.LocalDate type if you call getObject
and setObject
methods.
In obtuse language, the bottom of the JDBC 4.2 update spec confirms that article, with new mappings added to the getObject
and setObject
methods.
Convert
The spec also says new methods have been added to the java.sql.Date class to convert back and forth to java.time.LocalDate.
http://stackoverflow.com/questions/24650186/choosing-between-java-util-date-or-java-sql-date