Format ZonedDateTime to String – 将ZonedDateTime格式化为字符串

最后修改: 2018年 10月 22日

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

1. Overview

1.概述

In this quick tutorial, we’ll see how to convert a ZonedDateTime to a String.

在这个快速教程中,我们将看到如何将一个ZonedDateTime转换为String.

We’ll also look at how to parse a ZonedDateTime from a String.

我们还将看看如何从一个字符串中解析一个ZonedDateTime

2. Creating a ZonedDateTime

2.创建一个ZonedDateTime

First, we’ll start with a ZonedDateTime with a time zone of UTC. There are several ways we can accomplish this.

首先,我们将从一个ZonedDateTime开始,其时区为UTC。我们有几种方法可以实现这一点。

We can specify the year, month, day, etc:

我们可以指定年、月、日,等等。

ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));

We can also create a ZonedDateTime from the current date and time:

我们还可以从当前日期和时间创建一个ZonedDateTime

ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));

Or, we can create a ZonedDateTime from an existing LocalDateTime:

或者,我们可以从现有的LocalDateTime创建一个ZonedDateTime

LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));

3. ZonedDateTime to String

3.ZonedDateTimeString

Now, let’s convert our ZonedDateTime to a String. For this, we’ll use the DateTimeFormatter class.

现在,让我们将我们的ZonedDateTime转换为String.为此,我们将使用DateTimeFormatter类。

There are a few special formatters that we can use to display time zone data. The full list of formatters can be found here, but we’ll look at a few of the more common ones.

有一些特殊的格式化器,我们可以用来显示时区数据。格式化的完整列表可以在这里找到,但我们将看一下几个比较常见的格式。

If we want to display the time zone offset, we can use the formatter “Z” or “X”:

如果我们想显示时区偏移,我们可以使用格式化“Z “或 “X”

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss Z");
String formattedString = zonedDateTime.format(formatter);

This would give us a result like this:

这将给我们一个这样的结果。

02/01/2018 - 13:45:30 +0000

To include the time zone name, we can use a lowercase “z”:

为了包括时区名称,我们可以使用小写的 “z”:

DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss z");
String formattedString2 = zonedDateTime.format(formatter2);

The output of this would be:

这方面的输出将是。

02/01/2018 - 13:45:30 UTC

4. String to ZonedDateTime

4.StringZonedDateTime

This process can also work in reverse. We can take a string and convert it back into a ZonedDateTime.

这个过程也可以反向操作。我们可以将一个字符串转换为一个ZonedDateTime

One option to do this is by using the static parse() method of the ZonedDateTime class:

一种方法是使用ZonedDateTime类的静态parse()方法来实现:

ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00");

This method uses the ISO_ZONED_DATE_TIME formatter. There’s also an overloaded version of the method that takes a DateTimeFormatter parameter. However, the String has to contain a zone identifier or we’ll get an exception:

这个方法使用ISO_ZONED_DATE_TIME格式化。该方法还有一个重载版本,需要一个DateTimeFormatter参数。然而,这个字符串必须包含一个区域标识符,否则我们会得到一个异常。

assertThrows(DateTimeParseException.class, () -> 
  ZonedDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME));

A second option to obtain a ZonedDateTime from a String involves 2 steps: converting the String to a LocalDateTime, then this object to a ZonedDateTime:

字符串中获得ZonedDateTime的第二个选择包括两个步骤:将字符串转换为LocalDateTime,然后将此对象转换为ZonedDateTime:

ZoneId timeZone = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = LocalDateTime.parse("2011-12-03T10:15:30", 
  DateTimeFormatter.ISO_DATE_TIME).atZone(timeZone);

log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));

This indirect method simply combines the date-time with a zone id:

这种间接的方法只是将日期时间与区域ID相结合。

INFO: 2011-12-03T10:15:30+02:00[Europe/Athens]

To learn more about parsing String to dates, check out our more in-depth date parsing article.

要了解更多关于将字符串解析为日期的信息,请查看我们更深入的日期解析文章

5. Conclusion

5.结论

In this article, we’ve seen how to create a ZonedDateTime, and how to format it as a String.

在本文中,我们已经看到了如何创建一个ZonedDateTime,以及如何将其格式化为String.

We’ve also taken a quick look at how to parse a date time string and convert into a ZonedDateTime.

我们还快速浏览了如何解析一个日期时间字符串并转换为ZonedDateTime

The source code for this tutorial is available over on Github.

本教程的源代码可在Github上获得