1. Overview
1.概述
When working with dates in Java, we often see date/time values expressed as long values that denote the number of days, seconds, or milliseconds since the epoch, January 1, 1970, 00:00:00 GMT.
在 Java 中处理日期时,我们经常看到以 long 值表示的日期/时间值,这些值表示自 epoch(1970 年 1 月 1 日 00:00:00 GMT)以来的天数、秒数或毫秒数。
In this short tutorial, we’ll explore different ways of converting a long value to a date in Java. First, we’ll explain how to do this using core JDK classes. Then, we’ll showcase how to achieve the same objective using the third-party Joda-Time library.
在本简短教程中,我们将探讨在 Java 中将 long 值转换为日期的不同方法。首先,我们将解释如何使用 JDK 核心类来实现这一目标。然后,我们将展示如何使用第三方 Joda-Time 库实现相同的目标。
2. Using the Java 8+ Date-Time API
2.使用 Java 8+ 日期-时间 API
Java 8 is often praised for the new Date-Time API feature it brought to the Java landscape. This API was introduced mainly to cover the drawbacks of the old date API. So, let’s take a close look at what this API provides to answer our central question.
Java 8 为 Java 带来了新的日期-时间 API 功能,因此经常受到称赞。引入该 API 主要是为了弥补旧日期 API 的缺点。因此,让我们仔细看看该 API 提供了哪些功能来回答我们的核心问题。
2.1. Using the Instant Class
2.1.使用 Instant 类
The easiest solution would be using the Instant class introduced in the new Java 8 date-time API. This class describes a single instantaneous point on the timeline.
最简单的解决方案是使用 Instant 类,该类在 新 Java 8 日期时间 API 中引入。该类描述时间轴上的单个瞬时点。
So, let’s see it in practice:
那么,让我们来看看实际操作吧:
@Test
void givenLongValue_whenUsingInstantClass_thenConvert() {
Instant expectedDate = Instant.parse("2020-09-08T12:16:40Z");
long seconds = 1599567400L;
Instant date = Instant.ofEpochSecond(seconds);
assertEquals(expectedDate, date);
}
As shown above, we used the ofEpochSecond() method to create an object of the Instant class. Please bear in mind that we can use the ofEpochMilli() method as well to create an Instant instance using milliseconds.
如上所示,我们使用 ofEpochSecond() 方法创建了一个 Instant 类对象。请记住,我们也可以使用 ofEpochMilli() 方法使用毫秒创建一个 Instant 实例。
2.2. Using the LocalDate Class
2.2.使用 LocalDate 类
LocalDate is another option to consider when converting a long value to a date. This class models a classic date, such as 2023-10-17, without the time detail.
LocalDate 是将 长值转换为日期时需要考虑的另一个选项。该类模拟经典日期,如 2023-10-17,但不包含时间细节。
Typically, we can use the LocalDate#ofEpochDay method to achieve our objective:
通常,我们可以使用 LocalDate#ofEpochDay 方法来实现我们的目标:
@Test
void givenLongValue_whenUsingLocalDateClass_thenConvert() {
LocalDate expectedDate = LocalDate.of(2023, 10, 17);
long epochDay = 19647L;
LocalDate date = LocalDate.ofEpochDay(epochDay);
assertEquals(expectedDate, date);
}
The ofEpochDay() method creates an instance of the LocalDate class from the given epoch day.
ofEpochDay()方法根据给定的纪元日创建LocalDate类的实例。
3. Using the Legacy Date API
3.使用传统日期 API
Before Java 8, we would usually use the Date or Calendar classes from the java.util package to achieve our objective. So, let’s see how to use these two classes to convert a long value to a date.
在 Java 8 之前,我们通常使用 java.util 包中的 Date 或 Calendar 类来实现我们的目标。因此,让我们来看看如何使用这两个类将 long 值转换为日期。
3.1. Using the Date Class
3.1.使用 Date 类
The Date class denotes a specific instant in time with millisecond precision. As the name indicates, it comes with a host of methods that we can use to manipulate dates. It offers the easiest way to convert a long value to a date as it provides an overloaded constructor that accepts a parameter of long type.
Date 类以毫秒级的精度表示时间中的特定瞬间。正如其名称所示,该类包含大量方法,我们可以使用这些方法来操作日期。该类提供了将 long 值转换为日期的最简单方法,因为它提供了一个重载构造函数,可接受 long 类型的参数。
So, let’s see it in action:
那么,让我们来看看它的实际效果吧:
@Test
void givenLongValue_whenUsingDateClass_thenConvert() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expectedDate = dateFormat.parse("2023-10-15 22:00:00");
long milliseconds = 1689458400000L;
Date date = new Date(milliseconds);
assertEquals(expectedDate, date);
}
Please note that the Date class is outdated and belongs to the old API. So, it’s not the best way to go when working with dates.
请注意,日期类已经过时,属于旧的 API。因此,它不是处理日期的最佳方法。
3.2. Using the Calendar Class
3.2.使用 Calendar 类
Another solution would be to use the Calendar class from the old date API. This class provides the setTimeInMillis(long value) method that we can use to set the time to the given long value.
另一种解决方案是使用旧日期 API 中的 Calendar 类。该类提供了 setTimeInMillis(long value) 方法,我们可以使用该方法将时间设置为给定的 long 值。
Now, let’s exemplify the use of this method using another test case:
现在,让我们用另一个测试用例来示范这种方法的使用:
@Test
void givenLongValue_whenUsingCalendarClass_thenConvert() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expectedDate = dateFormat.parse("2023-07-15 22:00:00");
long milliseconds = 1689458400000L;
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
calendar.setTimeInMillis(milliseconds);
assertEquals(expectedDate, calendar.getTime());
}
Similarly, the specified long value denotes the number of milliseconds passed since the epoch.
类似地,指定的 long 值表示自纪元开始已过去的毫秒数。
4. Using Joda-Time
4.使用 Joda-Time
Lastly, we can use the Joda-Time library to tackle our challenge. First, let’s add its dependency to the pom.xml file:
最后,我们可以使用Joda-Time库来应对我们的挑战。首先,让我们将其 依赖关系添加到 pom.xml 文件中:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.5</version>
</dependency>
Similarly, Joda-Time provides its version of the LocalDate class. So, let’s see how we can use it to convert a long value to a LocalDate object:
同样,Joda-Time 也提供了自己版本的 LocalDate 类。因此,让我们看看如何使用它将 long 值转换为 LocalDate 对象:
@Test
void givenLongValue_whenUsingJodaTimeLocalDateClass_thenConvert() {
org.joda.time.LocalDate expectedDate = new org.joda.time.LocalDate(2023, 7, 15);
long milliseconds = 1689458400000L;
org.joda.time.LocalDate date = new org.joda.time.LocalDate(milliseconds, DateTimeZone.UTC);
assertEquals(expectedDate, date);
}
As illustrated, LocalDate offers a direct way to construct a date from a long value.
如图所示,LocalDate 提供了一种从 long 值构建日期的直接方法。
5. Conclusion
5.结论
In this short article, we explained in detail how to convert a long value to a date in Java.
在这篇短文中,我们详细介绍了如何在 Java 中将 long 值转换为日期。
First, we’ve seen how to do the conversion using built-in JDK classes. Then, we illustrated how to accomplish the same goal using the Joda-Time library.
首先,我们了解了如何使用 JDK 内置类进行转换。然后,我们说明了如何使用 Joda-Time 库实现同样的目标。
As always, the code used in this article can be found over on GitHub.