与 java.util.Date 和 java.sql.TimeStamp 冲突
我面临着一个独特的问题。
我正在查询一个数据类型(在 Oracle 中)为 DATE 的数据库字段。我正在从 resultSet 中检索值 rs.getObject("myDate") 。我们有两个具有相同配置的设置。在一种设置中,当我编写 rs.getObject("myDate").getClass() 时,我正在获取 java.sql.TimeStamp ,而在另一种设置中,它返回 java.util.Date 。我面临的问题是,当它返回日期对象时,它会忽略时间并仅返回日期部分。
I am facing a unique problem.
I am querying a database field which has a datatype (in oracle) as DATE . I am retrieving the value from resultSet as rs.getObject("myDate") . We have two setups which have identical configuration . On one setup when I write rs.getObject("myDate").getClass() , I am geeting java.sql.TimeStamp and on another setup it is returning java.util.Date. The problem I am facing is when it returns an object of date it is ignoring the time and returning only the date part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为可以使用此连接属性来控制此行为:
请参阅此论坛线程,例如:
http://forums.oracle.com/forums/thread.jspa?threadID=483903
I think this behaviour can be controlled using this connection property:
See this forum thread, for instance:
http://forums.oracle.com/forums/thread.jspa?threadID=483903
其他信息:java.sql.Timestamp 是一个 java.util.Date,因为它扩展了它。
日期也有时间,尽管它没有纳秒,这是唯一的区别。
可悲的是,这就是它的实施方式,但不是应该的方式。来自他们自己的文档:
这意味着他们懒得写一些封装。这会导致许多怪癖,例如哈希码在纳秒内不会改变以及不对称等于。
als info: java.sql.Timestamp IS a java.util.Date as it extends it.
Dates also have time, though it does not have nanoseconds, that's the only difference.
Sadly enough, that's the way it is implemented, but not the way it should be. From their own docs:
which means they were to lazy to write some encapsulation. This causes many quirks, like hashcodes not changing for nanoseconds and asymmetrical equals.