I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

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

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

2. Creating a ZonedDateTime

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

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 zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));

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

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

3. ZonedDateTime to String

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

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”:

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”:

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

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

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

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:

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:

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:

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

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

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

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

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

Leave a Reply

avatar
  Subscribe  
Notify of