Convert Between java.time.Instant and java.sql.Timestamp – 在java.time.instant和java.sql.Timestamp之间转换

最后修改: 2018年 11月 5日

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

1. Overview

1.概述

Both java.time.Instant and java.sql.Timestamp classes represent a point on the timeline in UTC. In other words, they represent the number of nanoseconds since the Java epoch.

java.time.Instantjava.sql.Timestamp类都表示UTC时间线上的一个点。换句话说,它们表示自Java纪元以来的纳秒数。

In this quick tutorial, we’ll convert one to the other by using built-in Java methods.

在这个快速教程中,我们将通过使用内置的Java方法将一个转换为另一个。

2. Converting Instant to Timestamp and Back

2.将Instant转换为Timestamp并返回

We can use Timestamp.from() to convert Instants into Timestamps:

我们可以使用Timestamp.from()Instants转换成Timestamps:

Instant instant = Instant.now();
Timestamp timestamp = Timestamp.from(instant);
assertEquals(instant.toEpochMilli(), timestamp.getTime());

And vice-versa, we can use Timestamp.toInstant() to convert Timestamps into Instants:

反之亦然,我们可以使用Timestamp.toInstant()Timestamps转换成Instants:

instant = timestamp.toInstant();
assertEquals(instant.toEpochMilli(), timestamp.getTime());

Either way, both the Instant and Timestamp represents the same point on the timeline.

无论哪种方式,InstantTimestamp都代表时间线上的同一个点。

Next, let’s have a look at the interaction between the two classes and the timezone.

接下来,让我们看看这两个类和时区之间的互动。

3. toString() Method Differences

3.toString()方法的区别

Invoking toString() on Instant and Timestamp behaves differently with respect to timezone. Instant.toString() returns the time in UTC timezone. On the other hand, Timezone.toString() returns the time in the local machine timezone.

InstantTimestamp上调用toString()在时区方面的行为是不同的。 Instant.toString()返回 UTC 时区的时间。另一方面,Timezone.toString()返回本地机器时区的时间。

Let’s see what we get when calling toString() on instant and timestamp respectively:

让我们看看在instanttimestamp上分别调用toString()会得到什么。

Instant (in UTC): 2018-10-18T00:00:57.907Z
Timestamp (in GMT +05:30): 2018-10-18 05:30:57.907

Here, timestamp.toString() resulted in a time which is 5 hours 30 minutes after the time returned by the instant.toString(). This is because the local machine’s timezone is at GMT +5:30 timezone.

这里,timestamp.toString()的结果是比instant.toString()返回的时间晚5小时30分钟。这是因为本地机器的时区是在GMT+5:30时区。

The output of the toString() method is different, but both the timestamp and instant represent the same point on the timeline.

toString()方法的输出是不同的,但是timestampinstant都代表时间线上的同一个点

We can also verify this by converting the Timestamp to the UTC time zone:

我们还可以通过将Timestamp转换为UTC时区来验证这一点。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
formatter = formatter.withZone(TimeZone.getTimeZone("UTC").toZoneId());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

assertThat(formatter.format(instant)).isEqualTo(df.format(timestamp));

4. Conclusion

4.总结

In this quick tutorial, we saw how to convert between java.time.Instant and java.sql.Timestamp classes in Java using built-in methods.

在这个快速教程中,我们看到了如何使用内置方法在Java中的java.time.Instantjava.sql.Timestamp类之间转换。

We also had a look at how the time zone affects how the output changes.

我们还看了一下时区是如何影响输出的变化的。

And, as always, the complete code examples are available over on GitHub.

而且,像往常一样,完整的代码示例可在GitHub上获得