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

查看:28
本文介绍了从 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?

我知道你可以使用 ResultSetgetDategetTimestamp 但这些方法返回 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.

推荐答案

大多数数据库供应商不支持 JDBC4.2 还没有.这个规范说新的java.使用现有方法 setObject(...)getObject() 将/应该支持像 LocalDate 这样的时间类型.无需和提供显式转换(无需更改 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天全站免登陆