-->
Skip to main content

ZonedDateTime parse() Method Example in Java

ZonedDateTime parse() Method Example in Java 

In this tutorial, we'll see how to use ZonedDateTime parse Method to parse a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris] to ZonedDateTime instance in java.


ZonedDateTime parse Method Example

Here is the brief intro about what is ZonedDateTime class in java.

ZonedDateTime

  • ZonedDateTime is an instance of date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
  • ZonedDateTime is an immutable representation of a date-time with a time-zone. 
  • This class is immutable and thread-safe.
There are two methods provided by ZonedDateTime to parse text string into ZonedDateTime instance.

1. public static ZonedDateTime parse(CharSequence text)

This parse method obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris]. The string must represent a valid date-time and is parsed using DateTimeFormatter.ISO_ZONED_DATE_TIME.

Parameters:

text - The text to parse such as "2007-12-03T10:15:30+01:00[Europe/Paris]", which will be not null

Returns:

the parsed zoned date-time instance, which is not null.

Throws:

It throws DateTimeParseException if the text cannot be parsed.

Example:


 package com.javacodegeek.post6.zoneddatetimeparse;

 import java.time.ZonedDateTime;

 public class ZonedDateTimeParseExample {
  public static void main(String[] args) {
	System.out.println("----Normal parse method executes from here----");
	// Obtains an instance of ZonedDateTime from a text string
	// such as 2007-12-03T10:15:30+01:00[Europe/Paris].
		
	// create an ZonedDateTime object
	ZonedDateTime time1 = ZonedDateTime.parse("2020-06-26T21:51:22.621+05:30[Asia/Calcutta]");
	// print result
	System.out.println(time1); // 2020-06-26T21:51:22.621+05:30[Asia/Calcutta]

	// create an ZonedDateTime object
	ZonedDateTime time = ZonedDateTime.parse("2020-06-26T21:51:22-05:00");
	// print result
	System.out.println(time);// 2020-06-26T21:51:22-05:00
	System.out.println("----Normal parse method ends here----");
  }
}

Output:

Let us run the above program, this will produce the following output 

----Normal parse method executes from here----
2020-06-26T21:51:22.621+05:30[Asia/Calcutta]
2020-06-26T21:51:22-05:00
----Normal parse method ends here----

2. public static ZonedDateTime parse(CharSequence text,DateTimeFormatter formatter)

This parse method obtains an instance of ZonedDateTime from a text string using a specific formatter. The text is parsed using the formatter, returning a date-time.

Parameters:

text - The text to parse, which should be not null will be the first parameter.
formatter - the formatter to use, which also should be not null will be second parameter.

Returns:

the parsed zoned date-time instance, which is not null.

Throws:

It throws DateTimeParseException if the text cannot be parsed.

Example:


 package com.javacodegeek.post6.zoneddatetimeparse;

 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;

 public class ZonedDateTimeParseWithFormatterExample {
	public static void main(String[] args) {

	 System.out.println("----Parse method with formatter executes from here----");
	 // Obtains an instance of ZonedDateTime
	 // from a text string using a specific formatter.
	 ZonedDateTime time2 = ZonedDateTime.parse("2020-06-26T21:51:22.621+05:30[Asia/Calcutta]",
	 		DateTimeFormatter.ISO_ZONED_DATE_TIME);
	 System.out.println(time2); // 2020-06-26T21:51:22.621+05:30[Asia/Calcutta]

	 // Using ISO_DATE_TIME Formatter
	 ZonedDateTime time3 = ZonedDateTime.parse("2020-06-26T21:51:22+05:30", DateTimeFormatter.ISO_DATE_TIME);
	 System.out.println(time3); // 2020-06-26T21:51:22+05:30

	 // Using ISO_OFFSET_DATE_TIME Formatter
	 ZonedDateTime time4 = ZonedDateTime.parse("2020-06-26T21:51:22+05:30", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
	 System.out.println(time4); // 2020-06-26T21:51:22+05:30

	 // Using ISO_ZONED_DATE_TIME Formatter with default timezone
	 ZonedDateTime time5 = ZonedDateTime.parse("2020-06-26T21:51:22.621+05:30[Asia/Calcutta]",
	 		DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(ZoneId.systemDefault()));
	 System.out.println(time5); // 2020-06-26T21:51:22.621+05:30[Asia/Calcutta]

	 // Using ISO_ZONED_DATE_TIME Formatter with US/Eastern timezone
	 ZonedDateTime time6 = ZonedDateTime.parse("2020-06-26T21:51:22.621+05:30[US/Eastern]",
	 		DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(ZoneId.of("US/Eastern")));
	 System.out.println(time6); // 2020-06-26T21:51:22.621-04:00[US/Eastern]

	 System.out.println("----Parse method with formatter ends here----");
  }
 }

Output:

Let us run the above program, this will produce the following output 

----Parse method with formatter executes from here----
2020-06-26T21:51:22.621+05:30[Asia/Calcutta]
2020-06-26T21:51:22+05:30
2020-06-26T21:51:22+05:30
2020-06-26T21:51:22.621+05:30[Asia/Calcutta]
2020-06-26T21:51:22.621-04:00[US/Eastern]
----Parse method with formatter ends here----

In the last two examples where we have used DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(ZoneId.systemDefault())  to parse string using default timezone and DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(ZoneId.of("US/Eastern")) to parse string passed using user-defined timezone. These cases may encounter when we are working with timezones in our projects.

Conclusion

In this quick tutorial, we have covered how we can use ZonedDateTime parse methods to parse text string to ZonedDateTime instance in java with System default timezone and user-defined timezone.

Happy Learning!!!

Comments