119

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

Gemtastic
  • 6,253
  • 6
  • 34
  • 53
  • possible duplicate of [Convert between LocalDate and sql.Date](http://stackoverflow.com/questions/29750861/convert-between-localdate-and-sql-date) – assylias Apr 20 '15 at 18:28
  • 2
    @assylias 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. – Madara's Ghost Apr 21 '15 at 18:40
  • 1
    @SecondRikudo the duplicate shows how to convert from LocalDate to Date AND vice versa, and therefore seems more general. – assylias Apr 21 '15 at 21:34

3 Answers3

217

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().

Gemtastic
  • 6,253
  • 6
  • 34
  • 53
  • 5
    This is a "horrible hack" according with the java.time.* author: https://stackoverflow.com/questions/33066904/localdate-to-java-util-date-and-vice-versa-simplest-conversion#comment72769769_33066935 – Dherik Dec 15 '17 at 17:16
  • 21
    You're reffering to questions about `java.util.Date` **NOT** `java.sql.Date` which this Question is about. – Gemtastic Dec 16 '17 at 09:57
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));
RichardK
  • 3,228
  • 5
  • 32
  • 52
  • 1
    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. – Gemtastic Jan 26 '17 at 08:41
  • Yes, it's to insert current date, for example when you create object and want to persist creation date. – RichardK Jan 26 '17 at 08:46
  • 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". – Gemtastic Jan 26 '17 at 08:51
-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".

juhist
  • 4,210
  • 16
  • 33
  • 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. – Gemtastic Mar 20 '15 at 14:55
  • 6
    Well, now you have two answers then, the more the merrier. I don't think there is anything wrong in answering your own posts. – juhist Mar 20 '15 at 15:03
  • I hope it will help someone in my place in the future. – Gemtastic Mar 20 '15 at 15:14
  • 2
    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. – Michael Piefel Feb 12 '16 at 09:16
  • `getDate()` is deprecated since Java 1.1 – BlueWizard Jan 25 '17 at 09:26
  • 1
    LocalDate.toDate() exists only in the Joda version of LocalDate, not in the standard library version. – Rörd Jun 08 '17 at 14:33