I am trying to use a java.util.Date
as input and then creating a query with it - so I need a java.sql.Date
.
I was surprised to find that it couldn't do the conversion implicitly or explicitly - but I don't even know how I would do this, as the Java API is still fairly new to me.
tl;dr
Don’t. Both classes are outmoded.
java.util.Date
&java.sql.Date
with JDBC 4.2 or later.Example query with
PreparedStatement
.Replacements:
Instant
instead ofjava.util.Date
Both represent a moment in UTC. but now with nanoseconds instead of milliseconds.
LocalDate
instead ofjava.sql.Date
Both represent a date-only value without a time of day and without a time zone.
Details
If you are trying to work with date-only values (no time-of-day, no time zone), use the
LocalDate
class rather thanjava.util.Date
.java.time
In Java 8 and later, the troublesome old date-time classes bundled with early versions of Java have been supplanted by the new java.time package. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
A SQL data type
DATE
is meant to be date-only, with no time-of-day and no time zone. Java never had precisely such a class† untiljava.time.LocalDate
in Java 8. Let's create such a value by getting today's date according to a particular time zone (time zone is important in determining a date as a new day dawns earlier in Paris than in Montréal, for example).At this point, we may be done. If your JDBC driver complies with JDBC 4.2 spec, you should be able to pass a
LocalDate
viasetObject
on aPreparedStatement
to store into a SQL DATE field.Likewise, use
ResultSet::getObject
to fetch from a SQL DATE column to a JavaLocalDate
object. Specifying the class in the second argument makes your code type-safe.In other words, this entire Question is irrelevant under JDBC 4.2 or later.
If your JDBC driver does not perform in this manner, you need to fall back to converting to the java.sql types.
Convert to java.sql.Date
To convert, use new methods added to the old date-time classes. We can call
java.sql.Date.valueOf(…)
to convert aLocalDate
.And going the other direction.
Converting from
java.util.Date
While you should avoid using the old date-time classes, you may be forced to when working with existing code. If so, you can convert to/from java.time.
Go through the
Instant
class, which represents a moment on the timeline in UTC. AnInstant
is similar in idea to ajava.util.Date
. But note thatInstant
has a resolution up to nanoseconds whilejava.util.Date
has only milliseconds resolution.To convert, use new methods added to the old classes. For example,
java.util.Date.from( Instant )
andjava.util.Date::toInstant
.To determine a date, we need the context of a time zone. For any given moment, the date varies around the globe by time zone. Apply a
ZoneId
to get aZonedDateTime
.† The java.sql.Date class pretends to be date-only without a time-of-day but actually does a time-of-day, adjusted to a midnight time. Confusing? Yes, the old date-time classes are a mess.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.In my case of picking date from JXDatePicker (java calender) and getting it stored in database as SQL Date type, below works fine ..
java.sql.Date date = new java.sql.Date(pickedDate.getDate().getTime());
where pickedDate is object of JXDatePicker
I am a novice: after much running around this worked. Thought might be useful
Method for comparing 2 dates (util.date or sql.date)
Here the example of converting Util Date to Sql date and ya this is one example what i am using in my project might be helpful to you too.