java.util.Date 和 java.sql.Date 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26979822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:59:41  来源:igfitidea点击:

What is the difference between java.util.Date and java.sql.Date?

javadate

提问by Chetan Joshi

What is the difference between java.util.Dateand java.sql.Date?

java.util.Date和 和有什么不一样java.sql.Date

Why can't we use java.util.Dateinstead of java.sql.Date?

为什么我们不能使用java.util.Date代替java.sql.Date

回答by kriyeta

Difference between java.util.Date and java.sql.Date in Java from site. It is well explained.

java.util.Date 和 java.sql.Date 之间的区别来自site。这是很好的解释。

Here are few differences on java.sql.Date and java.util.Date in Java in point format, if you any other difference between them which is worth noting then please post in comments :

以下是 Java 中点格式的 java.sql.Date 和 java.util.Date 的一些差异,如果您在它们之间有任何其他值得注意的差异,请在评论中发表:

  1. As per Javadoc java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.

  2. java.sql.Date just represent DATE without time information while java.util.Date represent both Date and Time information. This is the major differences why java.util.Date can not directly map to java.sql.Date.

  3. In order to suppress time information and to confirm with definition of ANSI SQL DATE type, the millisecond values used in java.sql.Date instance must be "normalized by setting the hours, minutes, seconds and milliseconds to zero in the timezone with with DATE instance is associated. In other words all time related information is removed from java.sql.Date class.

  1. 根据 Javadoc java.sql.Date 是毫秒值的薄包装器,JDBC 使用它来标识 SQL DATE 类型。

  2. java.sql.Date 只代表 DATE 没有时间信息,而 java.util.Date 代表日期和时间信息。这就是 java.util.Date 不能直接映射到 java.sql.Date 的主要区别。

  3. 为了抑制时间信息并与 ANSI SQL DATE 类型的定义进行确认,java.sql.Date 实例中使用的毫秒值必须“通过将时区中的小时、分钟、秒和毫秒设置为零来标准化。实例是关联的。换句话说,所有时间相关的信息都从 java.sql.Date 类中删除。