如何将 LocalDate 转换为 SQL Date Java? [英] How to convert LocalDate to SQL Date Java?
本文介绍了如何将 LocalDate 转换为 SQL Date Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 LocalDate 转换为 java.sql.Date
?
How do I convert a LocalDate to a java.sql.Date
?
尝试:
Record r = new Record();
LocalDate date = new Date(1967, 06, 22);
r.setDateOfBirth(new Date(date));
这失败了(不会编译),我能找到的只有 Joda 时间.
This fails (won't compile) and all I can find is Joda time stuff.
我使用的是 Java 8
I'm using Java 8
推荐答案
答案很简单;
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
是您在 JOOQ 中使用的记录,.getDate()
是从您的记录中获取日期的方法;假设您有一个名为 date_of_birth 的日期列,那么您的 get 方法应该被称为 getDateOfBirth()
.
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()
.
这篇关于如何将 LocalDate 转换为 SQL Date Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文