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
Convert LocalDate To Date in JavaStep 1: First Create the LocalDate object using either now() or of() method. now() method gets the current date where as of() creates the LocalDate object with the given year, month and day. Step 2: Next, Get the timezone from operating system using ZoneId. systemDefault() method.
The java. sql. Date represents the date value in JDBC. The constructor of this class accepts a long value representing the desired date and creates respective Date object.
The java. time. LocalDate. now() method obtains the current date from the system clock in the default time-zone.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With