将 java.sql.date 转换为 java.time.LocalDateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42414172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:37:15  来源:igfitidea点击:

Convert java.sql.date to java.time.LocalDateTime

java

提问by Ferre12

How do I convert a java.sql.Datereturned from a JDBC database to a java.time.LocalDateTime?

如何java.sql.Date将从 JDBC 数据库返回的 a转换为java.time.LocalDateTime

回答by Ferre12

It was actually easier than I thought. This worked for me: result.getTimestamp("value").toLocalDateTime()

这实际上比我想象的要容易。这对我有用: result.getTimestamp("value").toLocalDateTime()

回答by Julio Villane

If you are using Java 8 and Hibernate 5 there is no need of converting from/to java.sql.Date, only trust on Java's magic.

如果您使用的是 Java 8 和 Hibernate 5,则无需从/到 java.sql.Date 进行转换,只需相信 Java 的魔力。

The dependency you need to use is:

您需要使用的依赖项是:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-java8</artifactId>
  <version>5.1.0.Final</version>
</dependency>

In case you are using spring-boot there is no need to declare any extra dependency.

如果您使用的是 spring-boot,则无需声明任何额外的依赖项。

If you are using a previous version of Java the recommendation is to define a Converter like this one:

如果您使用的是以前版本的 Java,建议您定义一个像这样的转换器:

@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {

  @Override
  public Date convertToDatabaseColumn(LocalDate locDate) {
    return (locDate == null ? null : Date.valueOf(locDate));
  }

  @Override
  public LocalDate convertToEntityAttribute(Date sqlDate) {
    return (sqlDate == null ? null : sqlDate.toLocalDate());
  }
}

You can found more about the use of LocalDateTime in Java 8 in this link, and more about this kind of converters on this link.

你可以找到更多关于这在Java中8使用LocalDateTime的链接,以及更多关于这种转换器的这一对链接

回答by Alex Misiulia

Working method:

工作方法:

public LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
    return Instant.ofEpochMilli(dateToConvert.getTime())
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Non-working build for java.sql.Date (because it throws UnsupportedOperationException for instant() method):

java.sql.Date 的非工作构建(因为它为 Instant() 方法抛出 UnsupportedOperationException):

public LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
    return dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Credits to baeldungfor this answer on his site:

在他的网站上对baeldung的回答表示感谢:

回答by Ruchira

A possible way is to first convert it to java.time.LocalDate and then to java.time.LocalDateTime

一种可能的方法是先将其转换为 java.time.LocalDate,然后再转换为 java.time.LocalDateTime

sqlDate.toLocalDate().atStartOfDay()

Also you can specify a specific time using atTime() function

您也可以使用 atTime() 函数指定特定时间

sqlDate.toLocalDate().atStartOfDay(hour, miniute, second)