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.
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.
1. public static ZonedDateTime parse(CharSequence text)
Parameters:
Returns:
Throws:
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)
Parameters:
Returns:
Throws:
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.
Comments
Post a Comment
If you have any doubts, Please let me know.