Java ZonedDateTime.parse() Method with Examples
Tags: Java ZonedDateTime Java 8 DateTimeFormatter
In this Java core tutorial we learn how to use the java.time.ZonedDateTime.parse() method to convert a String to ZonedDateTime object.
Table of contents
- Convert String to ZonedDateTime in Java
- Convert String to ZonedDateTime with DateTimeFormatter in Java
Convert String to ZonedDateTime in Java
In Java we can use the ZonedDateTime.parse(CharSequence text) method to parse a String value to a ZonedDateTime object as the Java code below.
ZonedDateTimeParseExample1.java
import java.time.ZonedDateTime;
public class ZonedDateTimeParseExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-04-26T08:15:30+07:00[Asia/Ho_Chi_Minh]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-04-26T08:15:30+10:00[Australia/Sydney]");
ZonedDateTime zonedDateTime3 = ZonedDateTime.parse("2022-04-26T08:15:30+02:00[Europe/Paris]");
ZonedDateTime zonedDateTime4 = ZonedDateTime.parse("2022-04-26T08:15:30+09:00[Asia/Tokyo]");
System.out.println(zonedDateTime1);
System.out.println(zonedDateTime2);
System.out.println(zonedDateTime3);
System.out.println(zonedDateTime4);
}
}
2022-04-26T08:15:30+07:00[Asia/Ho_Chi_Minh]
2022-04-26T08:15:30+10:00[Australia/Sydney]
2022-04-26T08:15:30+02:00[Europe/Paris]
2022-04-26T08:15:30+09:00[Asia/Tokyo]
Convert String to ZonedDateTime with DateTimeFormatter in Java
With the ZonedDateTime.parse(CharSequence text, DateTimeFormatter formatter) method we can parse a String in specified format to ZonedDateTime object.
For example, in the following Java code we use the constant DateTimeFormatter.ISO_ZONED_DATE_TIME which defined ISO-8601 extended offset date-time format to parse the String.
ZonedDateTimeParseExample2.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeParseExample2 {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-26T08:15:30+07:00[Asia/Ho_Chi_Minh]", dateTimeFormatter);
System.out.println(zonedDateTime);
}
}
2022-04-26T08:15:30+07:00[Asia/Ho_Chi_Minh]
We also use the custom zoned date time format as below.
ZonedDateTimeParseExample3.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeParseExample3 {
public static void main(String... args) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-26 09:30:40 +07:00", dateTimeFormatter);
System.out.println("ZonedDateTime: " + zonedDateTime);
System.out.println("Zone: " + zonedDateTime.getZone());
}
}
ZonedDateTime: 2022-04-26T09:30:40+07:00
Zone: +07:00
Happy Coding 😊
Related Articles
Java ZonedDateTime.now() method with Examples
Java ZonedDateTime.of() Method with Examples
Java ZonedDateTime.ofInstant() Method with Examples
Java ZonedDateTime.ofStrict() Method with Examples
Java ZonedDateTime.getOffset() Method with Examples
Java ZonedDateTime.withEarlierOffsetAtOverlap() Method with Examples
Java ZonedDateTime.withLaterOffsetAtOverlap() Method with Examples
Java ZonedDateTime.getZone() Method with Examples
Java ZonedDateTime.withZoneSameLocal() Method with Examples
Java ZonedDateTime.withZoneSameInstant() Method with Examples
Java ZonedDateTime.withFixedOffsetZone() Method with Examples
Java ZonedDateTime.toLocalDateTime() Method with Examples
Java ZonedDateTime.toLocalDate() Method with Examples
Java ZonedDateTime.toLocalTime() Method with Examples
Java ZonedDateTime.getYear() Method with Examples
Java ZonedDateTime.getMonthValue() Method with Examples
Java ZonedDateTime.getMonth() Method with Examples
Java ZonedDateTime.getDayOfMonth() Method with Examples
Java ZonedDateTime.getDayOfYear() Method with Examples
Java ZonedDateTime.getDayOfWeek() Method with Examples
Java ZonedDateTime.getHour() Method with Examples
Java ZonedDateTime.getMinute() Method with Examples
Java ZonedDateTime.getSecond() Method with Examples
Java ZonedDateTime.getNano() Method with Examples
Java ZonedDateTime.withYear() Method with Examples
Java ZonedDateTime.withMonth() Method with Examples
Java ZonedDateTime.withDayOfMonth() Method with Examples
Java ZonedDateTime.withDayOfYear() Method with Examples
Java ZonedDateTime.withHour() Method with Examples
Java ZonedDateTime.withMinute() Method with Examples
Java ZonedDateTime.withSecond() Method with Examples
Java ZonedDateTime.withNano() Method with Examples
Java ZonedDateTime.plusYears() Method with Examples
Java ZonedDateTime.plusMonths() Method with Examples
Java ZonedDateTime.plusWeeks() Method with Examples
Java ZonedDateTime.plusDays() Method with Examples
Java ZonedDateTime.plusHours() Method with Examples
Java ZonedDateTime.plusMinutes() Method with Examples
Java ZonedDateTime.plusSeconds() Method with Examples
Java ZonedDateTime.plusNanos() Method with Examples
Java ZonedDateTime.minusYears() Method with Examples
Java ZonedDateTime.minusMonths() Method with Examples
Java ZonedDateTime.minusWeeks() Method with Examples
Java ZonedDateTime.minusDays() Method with Examples
Java ZonedDateTime.minusHours() Method with Examples
Java ZonedDateTime.minusMinutes() Method with Examples
Java ZonedDateTime.minusSeconds() Method with Examples
Java ZonedDateTime.minusNanos() Method with Examples
Java ZonedDateTime.format() Method with Examples
Java ZonedDateTime.toOffsetDateTime() Method with Examples
Java ZonedDateTime.equals() Method with Examples
Java ZonedDateTime.toString() Method with Examples
Java Compare two ZonedDateTime Values