java.util.Date 与 java.sql.Date [英] java.util.Date vs java.sql.Date

查看:32
本文介绍了java.util.Date 与 java.sql.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.Date vs java.sql.Date:何时使用哪个以及为什么?

java.util.Date vs java.sql.Date: when to use which and why?

推荐答案

恭喜您,您已经对 JDBC:日期类处理感到不满.

Congratulations, you've hit my favorite pet peeve with JDBC: Date class handling.

基本上数据库通常至少支持三种形式的日期时间字段,即日期、时间和时间戳.其中每一个在 JDBC 中都有一个对应的类,并且每一个都扩展了 java.util.Date.这三个中的每一个的快速语义如下:

Basically databases usually support at least three forms of datetime fields which are date, time and timestamp. Each of these have a corresponding class in JDBC and each of them extend java.util.Date. Quick semantics of each of these three are the following:

  • java.sql.Date 对应于SQL DATE,这意味着它存储年、月和日,而忽略时、分、秒和毫秒.此外,sql.Date 与时区无关.
  • java.sql.Time 对应于 SQL TIME,应该很明显,只包含关于小时、分钟、秒和毫秒的信息.
  • java.sql.Timestamp 对应于 SQL TIMESTAMP,它是精确到纳秒的日期(请注意,util.Date 仅支持毫秒!),具有可自定义的精度.
  • java.sql.Date corresponds to SQL DATE which means it stores years, months and days while hour, minute, second and millisecond are ignored. Additionally sql.Date isn't tied to timezones.
  • java.sql.Time corresponds to SQL TIME and as should be obvious, only contains information about hour, minutes, seconds and milliseconds.
  • java.sql.Timestamp corresponds to SQL TIMESTAMP which is exact date to the nanosecond (note that util.Date only supports milliseconds!) with customizable precision.

使用与这三种类型相关的 JDBC 驱动程序时最常见的错误之一是类型处理不正确.这意味着 sql.Date 是特定于时区的, sql.Time 包含当前的年月日等等.

One of the most common bugs when using JDBC drivers in relation to these three types is that the types are handled incorrectly. This means that sql.Date is timezone specific, sql.Time contains current year, month and day et cetera et cetera.

取决于字段的 SQL 类型,真的.PreparedStatement 具有所有三个值的 setter,#setDate()sql.Date#setTime() 的一个sql.Time 的 code> 和 sql.Timestamp#setTimestamp().

Depends on the SQL type of the field, really. PreparedStatement has setters for all three values, #setDate() being the one for sql.Date, #setTime() for sql.Time and #setTimestamp() for sql.Timestamp.

请注意,如果您使用 ps.setObject(fieldIndex, utilDateObject); 您实际上可以为大多数 JDBC 驱动程序提供一个普通的 util.Date ,它们会很乐意吞噬它好像它是正确的类型,但是当您之后请求数据时,您可能会注意到您实际上丢失了一些东西.

Do note that if you use ps.setObject(fieldIndex, utilDateObject); you can actually give a normal util.Date to most JDBC drivers which will happily devour it as if it was of the correct type but when you request the data afterwards, you may notice that you're actually missing stuff.

我所说的是将毫秒/纳秒保存为普通 long 并将它们转换为您正在使用的任何对象(强制性 joda-time 插件).一种可行的方法是将日期组件存储为一个长时间组件,将时间组件存储为另一个组件,例如现在是 20100221 和 154536123.这些幻数可以在 SQL 查询中使用,并且可以从数据库移植到另一个和会让你完全避免 JDBC/Java Date API:s 的这部分.

What I am saying that save the milliseconds/nanoseconds as plain longs and convert them to whatever objects you are using (obligatory joda-time plug). One hacky way which can be done is to store the date component as one long and time component as another, for example right now would be 20100221 and 154536123. These magic numbers can be used in SQL queries and will be portable from database to another and will let you avoid this part of JDBC/Java Date API:s entirely.

这篇关于java.util.Date 与 java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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