从LocalDate到java.sql.Date的正确方法是什么

时间:2013-09-25 21:04:46

标签: java jodatime

我有一个LocalDate,我正在尝试将其转换为java.sql.Date,以将其存储在数据库的日期字段中。我的问题是,当我将它转换为java.sql.Date时,由于我正在EST中运行的机器,它落后一天。我将Joda.DateTime设置为UTC,并使用millis构造函数创建java.sql.Date。我将调试手表的输出包括在内,以便您可以看到不同的值:

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"

从我假设的java.util.Date创建是因为我正在测试的机器是在EST中,而LocalDate是在UTC中,但我不确定解决这个问题的正确方法是什么。我意识到我可以获得本地时区并将其添加到millis,但这似乎是一个hacky修复。有没有一种正确的方法可以做到这一点,我只是缺席了?


使用millis更新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

4 个答案:

答案 0 :(得分:9)

正确的转换是:

    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());
    }

我在DAO辅助逻辑中使用它。在JDBC中你应该做

 public static final Calendar calendarUTC = Calendar.getInstance(
       TimeZone.getTimeZone("UTC"));

 d = rs.getDate(i, calendarUTC);

对于二传手也一样。

答案 1 :(得分:3)

让你有joda localdate

LocalDate loc = //...  

您可以通过以下方式将其转换为java.sql.Date区域的独立性:

Date date = new Date(loc .getYear() - 1900, loc .getMonthOfYear() - 1, loc .getDayOfMonth());

Date date = Date.valueOf(l.toString());

答案 2 :(得分:-1)

如果您通过搜索如何转换 Java 8 LocalDate找到此问题,请按以下步骤操作:

  

public static Date valueOf(LocalDate date)

     

从具有相同的LocalDate对象获取Date的实例   年,月,日值作为给定的LocalDate。该   提供的LocalDate被解释为本地时间的本地日期   区。参数:date - 要转换的LocalDateReturns:日期   objectThrows:NullPointerException - 如果date为nullSince:1.8

示例:

Date sqlDate = Date.valueOf(localDateObject);

答案 3 :(得分:-2)

您应该创建并使用java.sql.Timestamp,而不是java.sql.Date。 根据规范,java.sql.Date没有时间元素。

将LocalDate转换为时间戳:

Timestamp timestamp = new Timestamp(localDate.toDateMidnight().getMillis());

将LocalDateTime转换为时间戳:

Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());
相关问题