ZonedDateTime parse() method in Java with Examples - GeeksforGeeks

31 Dec, 2018
In ZonedDateTime class, there are two types of parse() method depending upon the parameters passed to it.
parse() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a string such as ‘2018-12-06T19:21:12.123+05:30[Asia/Calcutta]’ passed as parameter. The string must have a valid date-time and is parsed using DateTimeFormatter.ISO_ZONED_DATE_TIME.
Syntax:
public static ZonedDateTime parse(CharSequence text)

Parameters: This method accepts only one parameter text which is the text to parse in ZonedDateTime. It should not be null.
Return value: This method returns ZonedDateTime which is the parsed local date-time.
Exception: This method throws DateTimeParseException if the text cannot be parsed.
Below programs illustrate the parse() method:
Program 1:
// Java program to demonstrate// ZonedDateTime.parse() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // create an ZonedDateTime object        ZonedDateTime zoneddatetime2            = ZonedDateTime                  .parse(                      "2018-12-26T20:28:33.213+05:30[Asia/Calcutta]");          // print result        System.out.println("ZonedDateTime : "                           + zoneddatetime2);    }}
ZonedDateTime : 2018-12-26T20:28:33.213+05:30[Asia/Calcutta]

parse() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a string such as ‘2018-12-06T19:21:12.123+05:30[Asia/Calcutta]’ passed as parameter using a specific formatter.The date-time is parsed using a specific formatter.
Syntax:
public static ZonedDateTime parse(CharSequence text,
                                  DateTimeFormatter formatter)

Parameters: This method accepts two parameters text which is the text to parse and formatter which is the formatter to use.
Return value: This method returns ZonedDateTime which is the parsed local date-time.
Exception: This method throws DateTimeParseException if the text cannot be parsed.
Below programs illustrate the parse() method:
Program 1:
// Java program to demonstrate// ZonedDateTime.parse() method  import java.time.*;import java.time.format.*;  public class GFG {    public static void main(String[] args)    {          // create a formater        DateTimeFormatter formatter            = DateTimeFormatter                  .ISO_ZONED_DATE_TIME;          // create an ZonedDateTime object and        ZonedDateTime zdt            = ZonedDateTime                  .parse("2018-12-16T20:28:33.213+05:30[Asia/Calcutta]",                         formatter);          // print result        System.out.println("ZonedDateTime : "                           + zdt);    }}
ZonedDateTime : 2018-12-16T20:28:33.213+05:30[Asia/Calcutta]

References:https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#parse(java.lang.CharSequence)https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#parse(java.lang.CharSequence, java.time.format.DateTimeFormatter)
Java-Functions
Java-time package
Java-ZonedDateTime
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multithreading in Java
Multidimensional Arrays in Java