为什么java.sql.Timestamp扩展java.util.Date [英] Why does java.sql.Timestamp extend java.util.Date
问题描述
当我看到这个时,我感到困惑:
When I saw this I got baffled:
public class Timestamp extends java.util.Date {
//...
public boolean equals(java.lang.Object ts) {
if (ts instanceof Timestamp) {
return this.equals((Timestamp)ts);
} else {
return false;
}
}
public int hashCode() {
return super.hashCode();
}
它确实以粗体Note
记录在案
(请参见 https://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html )
It is indeed documented with a bold Note
(see https://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html)
对我来说,做出如此糟糕的决定可能是什么原因? 与java.util.Date对象进行比较时,为什么不调用super.equals(this)以使相等比较对称?
What could be the cause to make such a, to me, very bad decision? Why not call super.equals(this) when compared to a java.util.Date object to make the equal comparison symmetrical?
推荐答案
相反,请使用java.time
正如其他人所述:
Instead, use java.time
As others stated:
- 此类的继承设计不佳,存在缺陷.
- 现在该类已经成为历史遗产,由避免在java.time包之外找到所有旧的旧式日期时间类.
Avoid all the old legacy date-time classes found outside the java.time package.
对于数据库,请使用符合 JDBC 4.2或更高版本的驱动程序直接与数据库交换java.time对象.您可以忘记有关
java.sql.Timestamp
的全部信息.For databases, use a driver compliant with JDBC 4.2 or later to directly exchange java.time objects with your database. You can forget all about
java.sql.Timestamp
.存储:
Instant instant = Instant.now() ; myPreparedStatement.setObject( … , instant ) ;
检索:
Instant instant = myResultSet.getObject( … , Instant.class ) ;
关于java.time
java.time 框架已内置在Java 8及更高版本中.这些类取代了麻烦的旧版日期时间类,例如
Calendar
,&SimpleDateFormat
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.Joda-Time 项目,现在位于 java.time 类.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 和更高版本
- 内置.
- 具有捆绑的实现的标准Java API的一部分.
- Java 9添加了一些次要功能和修复.
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 许多java.time功能都向后移植到Java 6& 7在> ThreeTen-Backport 中./li>
- > ThreeTenABP 项目适应了<专门用于Android的em> ThreeTen-Backport (上面提到).
- 请参见 如何使用ThreeTenABP… .
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
> ThreeTen-Extra 项目扩展了java.time与其他班级.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如
Interval
,YearWeek
,YearQuarter
和更多.这篇关于为什么java.sql.Timestamp扩展java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Java SE 8 , Java SE 9 和更高版本