从 LocalDate 到 java.sql.Date 的正确方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19015186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to go from LocalDate to java.sql.Date
提问by Zipper
I have a LocalDate
that I'm trying to convert to a java.sql.Date
to store it in a Date field in the DB. My problem is that when I convert it to a java.sql.Date
it is one day behind due to the machine I'm running on being in EST. I setup Joda.DateTime
to be in UTC, and creating the java.sql.Date
via using the millis constructor. I'm including the output of the debug watches so you can see what the different values are:
我有一个LocalDate
我正在尝试将其转换为 ajava.sql.Date
以将其存储在数据库中的日期字段中。我的问题是,当我将其转换为 a 时java.sql.Date
,由于我在 EST 中运行的机器,它会落后一天。我设置Joda.DateTime
为UTC,并java.sql.Date
使用millis构造函数创建via。我包含了调试监视的输出,以便您可以看到不同的值是什么:
item.getDate().toDateTimeAtStartOfDay() = {org.joda.time.DateTime@6079}"2013-09-25T00:00:00.000Z"
item.getDate().toDateTimeAtStartOfDay().getMillis() = 1380067200000
new java.sql.Date(item.getDate().toDateTimeAtStartOfDay().getMillis()) = {java.sql.Date@6082}"2013-09-24"
item.getDate().getLocalMillis() = 1380067200000
new java.sql.Date(item.getDate().getLocalMillis()) = {java.sql.Date@6083}"2013-09-24"
new java.util.Date(item.getDate().getLocalMillis()) = {java.util.Date@6103}"Tue Sep 24 20:00:00 EDT 2013"
From the java.util.Date
creation I'm assuming it is because my machine I'm testing on is in EST, and the LocalDate is in UTC, but I'm not sure what the correct way to fix this is. I realize I could get the local timezone and add that to the millis, but that seems like a hacky fix. Is there a proper way to do this that I'm just missing?
从java.util.Date
创建我假设这是因为我正在测试的机器在 EST 中,而 LocalDate 在 UTC 中,但我不确定解决这个问题的正确方法是什么。我意识到我可以获取本地时区并将其添加到毫秒,但这似乎是一个hacky修复。有没有正确的方法来做到这一点,我只是想念?
Update with the millis for the java.util.Date
更新为毫秒 java.util.Date
new java.util.Date(item.getDate().getLocalMillis()).getTime() = 1380153600000
item.getDate().toDateTimeAtStartOfDay().getMillis() = 1380153600000
item.getDate() = {org.joda.time.LocalDate@5812}"2013-09-26"
new java.sql.Date(item.getDate().getLocalMillis()).getTime() = 1380153600000
new java.util.Date(item.getDate().getLocalMillis()) = {java.util.Date@5842}"Wed Sep 25 20:00:00 EDT 2013"
new java.util.Date(item.getDate().getLocalMillis()).getTime() = 1380153600000
采纳答案by leonbloy
The proper conversions are:
正确的转换是:
public static final DateTimeZone jodaTzUTC = DateTimeZone.forID("UTC");
// from java.sql.Date to LocalDate:
public static LocalDate dateToLocalDate(java.sql.Date d) {
if(d==null) return null;
return new LocalDate(d.getTime(), jodaTzUTC);
}
// from LocalDate to java.sql.Date:
public static java.sql.Date localdateToDate(LocalDate ld) {
if(ld==null) return null;
return new java.sql.Date(
ld.toDateTimeAtStartOfDay(jodaTzUTC).getMillis());
}
I use this in my DAO helper logic. In JDBC you should do
我在我的 DAO 助手逻辑中使用它。在 JDBC 你应该做
public static final Calendar calendarUTC = Calendar.getInstance(
TimeZone.getTimeZone("UTC"));
d = rs.getDate(i, calendarUTC);
And the same for the setter.
对于二传手也是如此。
回答by m88
You should create and use a java.sql.Timestamp, not a java.sql.Date. By spec, the java.sql.Date has no time element.
您应该创建和使用 java.sql.Timestamp,而不是 java.sql.Date。根据规范, java.sql.Date 没有时间元素。
To convert LocalDate to Timestamp:
要将 LocalDate 转换为时间戳:
Timestamp timestamp = new Timestamp(localDate.toDateMidnight().getMillis());
To convert LocalDateTime to Timestamp:
将 LocalDateTime 转换为时间戳:
Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());
回答by Ilya
Let's you have joda's local date
让我们约达的本地约会
LocalDate loc = //...
You can conver it to java.sql.Date
with independence of zone, in next ways:
您可以通过以下方式将其转换java.sql.Date
为区域独立性:
Date date = new Date(loc .getYear() - 1900, loc .getMonthOfYear() - 1, loc .getDayOfMonth());
or
或者
Date date = Date.valueOf(l.toString());
回答by Tomasz Mularczyk
If you found this question by searching how to convert Java 8LocalDate, here is how you do it:
如果您通过搜索如何转换Java 8LocalDate发现了这个问题,请按以下步骤操作:
public static Date valueOf(LocalDate date)
Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate. The provided LocalDate is interpreted as the local date in the local time zone. Parameters:date - a LocalDate to convertReturns:a Date objectThrows:NullPointerException - if date is nullSince:1.8
public static Date valueOf(LocalDate date)
从具有与给定 LocalDate 相同的年、月和月日值的 LocalDate 对象获取 Date 的实例。提供的 LocalDate 被解释为本地时区中的本地日期。参数:date - 要转换的 LocalDateReturns:a Date objectThrows:NullPointerException - 如果日期为 nullSince:1.8
example:
例子:
Date sqlDate = Date.valueOf(localDateObject);