How to convert LocalDate to SQL Date Java?
Asked Answered
P

3

121

How do I convert a LocalDate to a java.sql.Date?

Attempt:

Record r = new Record();
LocalDate date = new Date(1967, 06, 22);
r.setDateOfBirth(new Date(date));

This fails (won't compile) and all I can find is Joda time stuff.

I'm using Java 8

Pressurize answered 20/3, 2015 at 14:8 Comment(6)
possible duplicate of Convert between LocalDate and sql.DateCapella
@Capella How is the answer on the linked question any better than the ones given here? The other question should be closed as a duplicate of this, not vice versa.Kenton
@SecondRikudo the duplicate shows how to convert from LocalDate to Date AND vice versa, and therefore seems more general.Capella
Today: just don’t. When this question was asked nearly 10 years ago, you could have reasons to want a java.sql.Date even though that class was always a hack on top of the already poorly designed java.util.Date class. Since the advent of JDBC 4.2 and Hibernate 5, just store your LocalDate directly into your database and forget that the two problematic Date classes ever existed.Pazit
See Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2.Pazit
Yes this problem is legacy today, this was written when Java8 had just launched and with it the new java Date library. Still relevant when working with legacy systems though.Pressurize
P
220

The answer is really simple;

import java.sql.Date;
...
LocalDate locald = LocalDate.of(1967, 06, 22);
Date date = Date.valueOf(locald); // Magic happens here!
r.setDateOfBirth(date);

If you would want to convert it the other way around, you do it like this:

Date date = r.getDate();
LocalDate localD = date.toLocalDate();

r is the record you're using in JOOQ and .getDate() is the method for getting the date out of your record; let's say you have a date column called date_of_birth, then your get method should be called getDateOfBirth().

Pressurize answered 20/3, 2015 at 14:10 Comment(2)
This is a "horrible hack" according with the java.time.* author: #33067404Enchiridion
You're reffering to questions about java.util.Date NOT java.sql.Date which this Question is about.Pressurize
C
0

If you want current date:

Date date = Date.valueOf(LocalDate.now());

If you want a specific date:

Date date = Date.valueOf(LocalDate.of(1967, 06, 22));
Cowbind answered 26/1, 2017 at 8:31 Comment(3)
This is only faster if you want the current moment, if you want a specific date, as might be the case when you're dealing with someone's date of birth this won't work at all.Pressurize
Yes, it's to insert current date, for example when you create object and want to persist creation date.Cowbind
Which is important to note as the original question is all about creating a specific date. It's a note for all the copy-pasters out there. You CAN oneline the correct answer with Date date = Date.valueOf(LocalDate.of(1967, 06, 22)); if you want to do it "faster".Pressurize
H
-2

Have you tried using the toDate() method of LocalDate?

As in:

Record r = new Record();
LocalDate date = new Date(1967, 06, 22);
r.setDateOfBirth(date.toDate());

In general, it is a good idea to specify how it fails rather than just say "it fails".

Hypognathous answered 20/3, 2015 at 14:11 Comment(6)
To be honest, I only posted the question to post my answer so that you can find it when you google. I had to figure it on my own because goodle didn't have this question or answer.Pressurize
Well, now you have two answers then, the more the merrier. I don't think there is anything wrong in answering your own posts.Hypognathous
I hope it will help someone in my place in the future.Pressurize
The OP wrote “[…] all I can find is Joda time stuff. I'm unsing Java 8”. Your answer, however, is just more Joda stuff instead of a Java 8 answer.Noneffective
getDate() is deprecated since Java 1.1Thermic
LocalDate.toDate() exists only in the Joda version of LocalDate, not in the standard library version.Wire

© 2022 - 2024 — McMap. All rights reserved.