There are two date classes in Java, one in
java.util package and other in
java.sql package. Though both are known as Date class, their are some
difference between java.util.Date and java.sql.Date e.g. Former is used whenever a Date is required in Java application, while later is used to read and store
DATE SQL type from database. There is one more important difference is,
java.util.Date stores both date and time values, while
java.sql.date only stores date information, without any time part. As per javadoc,
java.sql.date is a thin wrapper around a millisecond value that allows JDBC to identify this as an
SQL DATE value. To conform with the definition of
SQL DATE, the millisecond values wrapped by a
java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated. See
SQL date vs Util date for few more differences. In this Java tutorial, we will learn how to convert a
java.util.Date to
java.sql.Date, as we often need to do this when storing values into database. Since
java.util.Date is standard way to represent date and time in Java, I only keep
java.sql.Date up-to
JDBC or
DAO Layer, by the way if you need date with time, then use
SQL TIMESTAMP type instead of
DATE. It's also notable that
java.sql.Date is subclass of java.util.Date, but it doesn't mean you can pass around this in place of
java.util.Date, because it violates
Liskov Substitution principle. All time related methods of
java.sql.Date throws
IllegalArgumentException, as shown below