Parse String to ZonedDateTime in Java

Java 8 ZonedDateTime class represents an instant in the universal timeline with the timezone information. In this tutorial, we will learn to parse a string to ZonedDateTime object using its parse() method.

1. Parsing a Date Time with ZonedDateTime.parse()

Java program to convert a given string into ZonedDateTime instance. After parsing the date, we are converting the timestamp to our local timezone.

final static DateTimeFormatter formatter  = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a z");

ZonedDateTime zdtWithZoneOffset = ZonedDateTime.parse("2019-03-27 10:15:30 am -05:00", formatter);

ZonedDateTime zdtInLocalTimeline = zdtWithZoneOffset.withZoneSameInstant(ZoneId.systemDefault());

System.out.println(zdtWithZoneOffset);
System.out.println(zdtInLocalTimeline);

Program output.

2019-03-27T10:15:30-05:00
2019-03-27T20:45:30+05:30[Asia/Calcutta]

2. Converting String to Local or Preferred Timezone

Sometimes we will have the date-time String without the Zone information, for example, a customer sent us an excel sheet with sale records. In such cases, we may want to parse the dates with a preferred timezone.

One good way of doing such parsing is to first parse the string to LocalDateTime and then add the zone information to the instance.

LocalDateTime ldt = LocalDateTime
    .parse("2019-03-27 10:15:30 am", formatterWithoutZone);

ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());

System.out.println(zdt);

3. Date Patterns

3.1. Default Pattern

The default date pattern is DateTimeFormatter.ISO_ZONED_DATE_TIME.

The format consists of:

  • The ISO_LOCAL_DATE
  • The letter ‘T’. Parsing is case insensitive.
  • The ISO_LOCAL_TIME
  • The offset ID. If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive.
  • If the zone ID is not available or is a ZoneOffset then the format is complete.
  • An open square bracket ‘[‘.
  • The zone ID. This is not part of the ISO-8601 standard. Parsing is case sensitive.
  • A close square bracket ‘]’.

3.2. Custom Patterns

There are other useful built-in patterns that we can use to parse dates to ZonedDateTime instances.

  • RFC_1123_DATE_TIME – The RFC-1123 date-time formatter, such as ‘Tue, 3 Jun 2008 11:05:30 GMT‘.
  • ISO_OFFSET_DATE_TIME – The ISO date-time formatter that formats or parses a date-time with an offset, such as ‘2011-12-03T10:15:30+01:00‘.
  • ISO_ZONED_DATE_TIME – The ISO-like date-time formatter that formats or parses a date-time with offset and zone, such as ‘2011-12-03T10:15:30+01:00[Europe/Paris]’.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode