java.util.Date vs java.sql.Date – java.util.Date vs java.sql.Date

最后修改: 2018年 8月 6日

中文/混合/英文(键盘快捷键:t)

 

1. Overview

1.概述

In this tutorial, we’re going to compare two date classes: java.util.Date and java.sql.Date.

在本教程中,我们将比较两个日期类。java.util.Datejava.sql.Date

Once we complete the comparison, it should be clear which one to use and why.

一旦我们完成比较,就应该清楚使用哪一个,以及为什么。

2. java.util.Date

2.java.util.Date

The java.util.Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time). The class is used to keep coordinated universal time (UTC).

java.util.Date类代表了一个特定的时间时刻,精度为毫秒,从1970年1月1日00:00:00 GMT(纪元时间)开始。该类用于保持协调世界时(UTC)。

We can initialize it in two ways.

我们可以通过两种方式初始化它。

By calling the constructor:

通过调用构造函数。

Date date = new Date();

which will create a new date object with time set to the current time, measured to the nearest millisecond.

这将创建一个新的date对象,时间设置为当前时间,测量到最近的毫秒。

Or by passing a number of milliseconds since the epoch:

或者通过传递一个自纪元以来的毫秒数。

long timestamp = 1532516399000; // 25 July 2018 10:59:59 UTC
Date date = new Date(timestamp);

Let’s note that other constructors, present prior to Java 8, are deprecated now.

让我们注意到,在Java 8之前存在的其他构造函数,现在已经被废弃了。

However, Date has a number of issues and overall its usage isn’t recommended anymore.

然而,Date有许多问题,总体上不建议再使用它

It’s mutable. Once we initialize it, we can change its internal value. For example, we can call the setTime method:

它是可变的。一旦我们初始化它,我们就可以改变它的内部值。例如,我们可以调用setTime方法。

date.setTime(0); // 01 January 1970 00:00:00

To learn more about the advantages of immutable objects, check out this article: Immutable Objects in Java.

要了解更多关于不可变对象的优势,请查看这篇文章。Immutable Objects in Java

It also doesn’t handle all dates very well. Technically, it should reflect coordinated universal time (UTC). However, that depends on an operating system of the host environment.

它也不能很好地处理所有日期。从技术上讲,它应该反映协调世界时(UTC)。然而,这取决于主机环境的操作系统。

Most modern operating systems use 1 day = 24h x 60m x 60s = 86400 seconds, which as we can see, doesn’t take the “leap second” into account.

大多数现代操作系统使用1天=24h x 60m x 60s = 86400秒,我们可以看到,这并没有考虑到 “闰秒”。

With the introduction of Java 8, java.time package should be used. Prior to Java 8, an alternative solution was available – Joda Time.

随着Java 8的引入,应该使用java.time。在 Java 8 之前,有一个替代解决方案 – Joda Time

3. java.sql.Date

3.java.sql.Date

The java.sql.Date extends java.util.Date class.

java.sql.Date扩展java.util.Date类。

Its main purpose is to represent SQL DATE, which keeps years, months and days. No time data is kept.

它的主要目的是表示SQL DATE,它保留年、月、日。不保留时间数据。

In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero.

事实上,日期被存储为自1970年1月1日00:00:00格林尼治标准时间起的毫秒,时间部分被规范化,即设置为零。

Basically, it’s a wrapper around java.util.Date that handles SQL specific requirements. java.sql.Date should be used only when dealing with databases.

基本上,它是一个围绕java.util.Date的包装器,可以处理SQL的具体要求。java.sql.Date应该只在处理数据库时使用。

However, as java.sql.Date doesn’t hold timezone information, the timezone conversion between our local environment and database server depends on an implementation of JDBC driver. This adds another level of complexity.

然而,由于java.sql.Date没有保存时区信息,我们本地环境和数据库服务器之间的时区转换取决于JDBC驱动程序的实现。这就增加了另一个层次的复杂性。

Finally, let’s note, in order to support other SQL data types: SQL TIME and SQL TIMESTAMP, two other java.sql classes are available: Time and Timestamp.

最后,让我们注意一下,为了支持其他SQL数据类型。SQL TIME和SQL TIMESTAMP,还有两个java.sql类可用。TimeTimestamp

The latter, even though extends from java.util.Date, supports nanoseconds.

后者尽管是从java.util.Date中延伸出来的,但它支持纳秒级。

4. Conclusion

4.结论

Class java.util.Date stores a date-time value as milliseconds since the epoch. java.sql.Date stores a date only value and is commonly used in JDBC.

java.util.Date类将日期时间值存储为自纪元以来的毫秒。java.sql.Date 存储一个仅有日期的值,通常用于JDBC。

Handling dates is tricky. We need to remember about special cases: leap seconds, different timezones etc. When dealing with JDBC we can use java.sql.Date with caution.

处理日期是很棘手的。我们需要记住一些特殊情况:闰秒、不同的时区等等。在处理JDBC时,我们可以谨慎地使用java.sql.Date

If we’re going to use java.util.Date, we need to remember about its shortcomings. If using Java 8 then better not to use java.util.Date at all.

如果我们要使用java.util.Date,我们需要记住它的缺点。如果使用Java 8,那么最好根本不要使用java.util.Date