Convert between String and Timestamp – 字符串和时间戳之间的转换

最后修改: 2018年 11月 20日

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

1. Overview

1.概述

Timestamp is one of a few legacy date-time objects in Java.

Timestamp是Java中少数遗留的日期时间对象之一。

In this tutorial, we’ll see how to parse from a String value to a Timestamp object and how to format a Timestamp object to a String.

在本教程中,我们将看到如何从一个字符串值解析为一个时间戳对象,以及如何将一个时间戳对象格式化为字符串。

And since Timestamp relies on a Java-proprietary format, we’ll see how to effectively adapt.

而由于Timestamp依赖于Java专有的格式,我们将看到如何有效地适应。

2. Parse a String to a Timestamp

2.将一个字符串解析为一个时间戳

2.1. Standard Format

2.1 标准格式

The simplest way to parse a String to a Timestamp is its valueOf method:

字符串解析为时间戳的最简单方法是其valueOf方法:

Timestamp.valueOf("2018-11-12 01:02:03.123456789")

And when our String is in JDBC timestamp format – yyyy-m[m]-d[d] hh:mm:ss[.f…] – then it’s pretty simple.

当我们的String是JDBC的时间戳格式 – yyyy-m[m]-d[d] hh:mm:ss[.f…] – 那么它就非常简单了。

We can interpret that pattern like this:

我们可以这样解释这一模式。

Pattern Description Example
yyyy Represents the year, and it’s mandatory to have four digits for it. 2018
m[m] For the month component, we must have either one or two digits (from 1 to 12). 1, 11
d[d] For the day of month value, we must have either one or two digits (from 1 to 31). 7, 12
hh Stands for the hour of the day, with allowed values from 0 to 23. 01, 16
mm Stands for minutes of the hour, with allowed values from 0 to 59. 02, 45
ss Stands for seconds in the minute, with allowed values from 0 to 59. 03, 52
[.f…] Represents optional fractions of a second, can be up to nanoseconds precision so the allowed values are from 0 to 999999999. 12, 1567, 123456789

2.2. Alternative Formats

2.2.替代格式

Now, if it isn’t in JDBC timestamp format, then luckily, valueOf also takes a LocalDateTime instance.

现在,如果它不是JDBC的时间戳格式,那么幸运的是,valueOf也需要一个LocalDateTime实例。

This means we can take a date in any format, we just need to first convert it into a LocalDateTime:

这意味着我们可以接受任何格式的日期,我们只需要首先将其转换为LocalDateTime

String pattern = "MMM dd, yyyy HH:mm:ss.SSSSSSSS";
String timestampAsString = "Nov 12, 2018 13:02:56.12345678";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.from(formatter.parse(timestampAsString));

And then we can use valueOf  we did before:

然后我们可以使用我们之前的valueOf

Timestamp timestamp = Timestamp.valueOf(localDateTime);
assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());

Note in passing that, unlike a Date object, a Timestamp object is capable of storing fractions of a second.

请注意,Date对象不同,Timestamp对象能够存储几分之一秒。

3. Format a Timestamp as a String

3.将时间戳格式化为字符串

To format a Timestamp, we’ll have the same challenge since it’s default format is the proprietary JDBC timestamp format:

要格式化Timestamp,我们将面临同样的挑战,因为它的默认格式是专有的JDBC时间戳格式。

assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());

But, again, using an intermediary conversion, we can format the resulting String to a different date and time pattern, like the ISO-8601 standard:

但是,同样地,使用一个中间转换,我们可以将产生的String格式化为不同的日期和时间模式,如ISO-8601标准。

Timestamp timestamp = Timestamp.valueOf("2018-12-12 01:02:03.123456789");
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
 
String timestampAsString = formatter.format(timestamp.toLocalDateTime());
assertEquals("2018-12-12T01:02:03.123456789", timestampAsString);

4. Conclusion

4.总结

In this article, we saw how to convert between String and Timestamp objects in Java. Also, we saw how to use LocalDateTime conversion as an intermediary step in order to convert to, and from, different date and time patterns.

在这篇文章中,我们看到了如何在Java中的StringTimestamp对象之间转换。此外,我们还看到了如何使用LocalDateTime转换作为中间步骤,以便转换为不同的日期和时间模式。

And make sure to find all of these examples and snippets over on GitHub.

并确保在GitHub上找到所有这些例子和片段