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

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

问题描述

java.util.Date vs java.sql.Date :何时使用,为什么?恭喜,您已经用JDBC打开了我最喜欢的宠物小孩:日期课程处理。


$ b

解决方案$ b

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




  • java.sql.Date 对应于SQL DATE这意味着它会存储年,月和日,而小时,分,秒和毫秒将被忽略。另外 sql.Date 不绑定到时区。

  • java.sql.Time 对应于SQL TIME,应该是显而易见的,只包含有关小时,分钟,秒和毫秒的信息。

  • java.sql.Timestamp 对应于SQL TIMESTAMP,它是确切的日期到纳秒(注意 util.Date 只支持毫秒!),具有可定制的精度。 >


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



最后:使用哪一个?



真的是取决于字段的SQL类型。 PreparedStatement 具有所有三个值的setter, #setDate() sql的值。日期 #setTime() for sql.Time #setTimestamp() for sql.Timestamp



请注意,如果您使用您可以为大多数JDBC驱动程序提供正常的 util.Date ps.setObject(fieldIndex,utilDateObject); 很高兴地吞噬它,好像它是正确的类型,但是当你以后请求数据时,你可能会注意到你实际上是缺少的东西。



我真的在说所有的日期都不应该被使用。



我所说的保存毫秒/纳秒作为简单的长度,并将它们转换为你正在使用的任何对象( 强制性的joda-time插件 )。可以做的一个奇怪的方法是将日期组件作为另一个长时间组件存储,例如现在将是20100221和154536123.这些魔术数字可用于SQL查询,并将从数据库移植到另一个,将让您完全避免这部分JDBC / Java Date API。


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

解决方案

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

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 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.

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.

Finally: Which one to use?

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.

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.

I'm really saying that none of the Dates should be used at all.

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 vs java.sql.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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