Java's java.sql.Date

Jakob Jenkov
Last update: 2014-06-23

Java's java.sql.Date class is used in the JDBC API. If you need to set a date on a java.sql.PreparedStatement or get a date from a java.sql.ResultSet, you will interact with java.sql.Date.

Actually, java.sql.Date extends java.util.Date, so anything you can do with a java.util.Date you can also do with a java.sql.Date. Check out java.util.Date for more details.

Here is how to instantiate a java.sql.Date

long time = System.currentTimeMillis();
java.sql.Date date = new java.sql.Date(time);

The biggest difference between java.sql.Date and java.util.Date is that the java.sql.Date only keeps the date, not the time, of the date it represents. So, for instance, if you create a java.sql.Date using the date and time 2009-12-24 23:20, then the time (23:20) would be cut off. If you need to keep the time, use a java.sql.Timestamp instead.

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction




















Advertisements

High-Performance
Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next