LocalDateTime parse() method in Java with Examples - GeeksforGeeks

31 Dec, 2018
In LocalDateTime class, there are two types of parse() method depending upon the parameters passed to it.
parse() method of a LocalDateTime class used to get an instance of LocalDateTime from a string such as ‘2018-10-23T17:19:33’ passed as parameter.The string must have a valid date-time and is parsed using DateTimeFormatter.ISO_LOCAL_DATE_TIME.
Syntax:
public static LocalDateTime parse(CharSequence text)

Parameters: This method accepts only one parameter text which is the text to parse in LocalDateTime. It should not be null.
Return value: This method returns LocalDateTime 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// LocalDateTime.parse() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // create an LocalDateTime object        LocalDateTime lt            = LocalDateTime                  .parse("2018-12-30T19:34:50.63");          // print result        System.out.println("LocalDateTime : "                           + lt);    }}
LocalDateTime : 2018-12-30T19:34:50.630

parse() method of a LocalDateTime class used to get an instance of LocalDateTime from a string such as ‘2018-10-23T17:19:33’ passed as parameter using a specific formatter.The date-time is parsed using a specific formatter.
Syntax:
public static LocalDateTime 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 LocalDateTime 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// LocalDateTime.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_LOCAL_DATE_TIME;          // create an LocalDateTime object and        LocalDateTime lt            = LocalDateTime                  .parse("2018-12-30T19:34:50.63",                         formatter);          // print result        System.out.println("LocalDateTime : "                           + lt);    }}
LocalDateTime : 2018-12-30T19:34:50.630

References:https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#parse(java.lang.CharSequence)https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#parse(java.lang.CharSequence, java.time.format.DateTimeFormatter)
Java-Functions
Java-LocalDateTime
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
Difference between Abstract Class and Interface in Java
How to remove an element from ArrayList in Java?