Java util.date 和 sql.date 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18078004/
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
difference between util.date and sql.date?
提问by Dipak M.
Can many one help me?
许多人可以帮助我吗?
import java.util.Date;
public class DateDemo {
public static void main(String [] p)
{
java.util.Date date1 = new Date();
@SuppressWarnings("deprecation")
java.sql.Date date2 = new java.sql.Date(2013, 12, 22);
System.out.println(date1.compareTo(date2));
}
}
回答by zxc
As per Javadoc java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.
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.
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.
根据 Javadoc java.sql.Date 是一个围绕毫秒值的瘦包装器,JDBC 使用它来标识 SQL DATE 类型。
java.sql.Date 只代表 DATE 没有时间信息,而 java.util.Date 代表日期和时间信息。这就是 java.util.Date 不能直接映射到 java.sql.Date 的主要区别。
为了抑制时间信息并与 ANSI SQL DATE 类型的定义进行确认,java.sql.Date 实例中使用的毫秒值必须“通过将时区中的小时、分钟、秒和毫秒设置为零来标准化。实例是关联的。换句话说,所有时间相关的信息都从 java.sql.Date 类中删除。
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBWDwBD0
阅读更多:http: //javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBWDwBD0
回答by Mohsin Shaikh
Date from util package has is combination of date and time while Date from SQL package only represent only Date part. to be precise Date contains year, month and day information while Time means hour, minute and second information. java.util.Date contains all year, month, day, hour, minute and second information. In fact java.sql.Time and java.sql.TimeStamp which represents TIME and TIMESTAMP type of SQL database is more close to java.util.Date, It extends java.util.DATE and if you are using java.util.DATE in your Class to represent DATE value its better to use TIMESTAMP type in Database and java.sql.Time in JDBC or DAO code.
来自 util 包的日期是日期和时间的组合,而来自 SQL 包的日期仅代表日期部分。准确地说,Date 包含年、月和日信息,而 Time 表示小时、分钟和秒信息。java.util.Date 包含所有年、月、日、小时、分钟和秒信息。实际上,代表 TIME 和 TIMESTAMP 类型的 SQL 数据库的 java.sql.Time 和 java.sql.TimeStamp 更接近 java.util.Date,它扩展了 java.util.DATE,如果您在使用 java.util.DATE你的类来表示 DATE 值,最好在数据库中使用 TIMESTAMP 类型,在 JDBC 或 DAO 代码中使用 java.sql.Time。
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBmF4lBd
阅读更多:http: //javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBmF4lBd