How to convert string to LocalDate in Java

A LocalDate represents a date without time and timezone information. This class was introduced in Java 8 new date and time API for handling dates in ISO-8601 format (yyyy-MM-dd). Unlike the legacy Date class, it doesn't store any time or timezone.

In this article, you'll learn how to convert a date string to an instance of LocalDate 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 string in ISO-8601 format — ISO_LOCAL_DATE and parses it directly to an instance of LocalDate.

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

// default ISO-8601 formatted string
String str = "2017-06-25";

// parse string to `LocalDate`
LocalDate date = LocalDate.parse(str);

// print `LocalDate`
System.out.println("Parsed LocalDate: " + date);

Here is what the output looks like:

Parsed LocalDate: 2017-06-25

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

// parse custom date strings
LocalDate date = LocalDate.parse("December 15, 2019", DateTimeFormatter.ofPattern("MMMM dd, yyyy"));
LocalDate date2 = LocalDate.parse("07/17/2019", DateTimeFormatter.ofPattern("MM/dd/yyyy"));
LocalDate date3 = LocalDate.parse("02-Aug-1989", DateTimeFormatter.ofPattern("dd-MMM-yyyy"));

// print `LocalDate` instances

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


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.