How to convert string to LocalDateTime in Java

The LocalDateTime class, introduced in Java 8 new date and time API, represents both local date and time without timezone in ISO-8601 format (yyyy-MM-ddTHH:mm:ss).

LocalDateTime is the most commonly used class from Java 8 new data and time API to handle dates and times together. It provides a wide range of utility methods for different types of date and time operations.

In this article, you'll learn how to convert a date-time string to an instance of LocalDateTime in Java 8 and higher.

The new date and time API provides the parse() method for parsing a string to date. By default, this method accepts a date-time string in ISO-8601 format and parses it directly to an instance of LocalDateTime.

The following example demonstrates how you can convert the default ISO-8601 formatted string to LocalDateTime using the parse() method:

// ISO-8601 formatted string
String str = "2009-12-02T11:25:25";

// parse string to `LocalDateTime`
LocalDateTime dateTime = LocalDateTime.parse(str);

// print `LocalDateTime`
System.out.println("Parsed LocalDateTime: " + dateTime);

Here is what the output looks like:

Parsed LocalDateTime: 2009-12-02T11:25:25

To parse a date-time string that is not in ISO-8601 format, you need to pass an instance of DateTimeFormatter to explicitly specify the date-time string pattern as shown below:

// parse custom date-time strings
LocalDateTime dateTime = LocalDateTime.parse("Jan 15, 2019 20:12",
        DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm"));
LocalDateTime dateTime2 = LocalDateTime.parse("09/25/2017 12:55 PM",
        DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a"));
LocalDateTime localDate3 = LocalDateTime.parse("02-August-1989 11:40:12.450",
        DateTimeFormatter.ofPattern("dd-MMMM-yyyy HH:mm:ss.SSS"));

// print `LocalDate` instances
System.out.println(dateTime);
System.out.println(dateTime2);
System.out.println(localDate3);

The above code snippet will print the following on the console:

2019-01-15T20:12
2017-09-25T12:55
1989-08-02T11:40:12.450

Check out how to convert a string to date in Java guide for more string-to-date conversion examples.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.