从ResultSet获取日期以与java.time类一起使用 [英] Getting the date from a ResultSet for use with java.time classes

查看:163
本文介绍了从ResultSet获取日期以与java.time类一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何都要获得 java.time (Java 8中的新增功能)兼容时间类 ResultSet

Is there anyway to get a java.time (new in Java 8) compatible time class out of a ResultSet?

我知道你可以使用 ResultSet getDate getTimestamp 但这些方法返回 java.sql.Date / java.sql.Timestamp 现在已弃用的对象,因此使用它们以创建 ZonedDateTime 或类似物品似乎是不好的做法。

I am aware you can use ResultSet's getDate or getTimestamp but these method return java.sql.Date / java.sql.Timestamp objects which are now deprecated so it seems like bad practice to use them in order to create a ZonedDateTime or similar.

推荐答案

大多数数据库供应商不支持 JDBC 4.2 。这个规范说新的 java.time 将使用现有方法支持 LocalDate 等类型 setObject(...) getObject()。不需要和提供显式转换(无API更改)。

Most database vendors don't support JDBC 4.2 yet. This specification says that the new java.time-types like LocalDate will/should be supported using the existing methods setObject(...) and getObject(). No explicit conversion is required and offered (no API-change).

缺少支持的解决方法可以是手动转换,如德比邮件列表

A workaround for the missing support can be manual conversion as described on the Derby-mailing list.

类似于:

LocalDate birthDate = resultSet.getDate("birth_date").toLocalDate();

如您所见,这些转换使用非弃用类型 java。 sql.Date 等,另见 javadoc

As you can see, these conversions use the non-deprecated types java.sql.Date etc., see also the javadoc.

这篇关于从ResultSet获取日期以与java.time类一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆