How to convert String to LocalDate Java 8

How to convert String to LocalDate Java 8 are popular questions in any java interviews. We can do it quite easily by using the parse() method of the LocalDate and DateTimeFormatter classes.

Let’s begin:

Convert a String into to LocalDate using below syntax:

if your string is formatted like ISO_LOCAL_DATE (format=yyyy-MM-dd), you can parse the String directly without the help of DateTimeFormatter.

Popular date patterns

yyyy-MM-dd pattern (ISO_LOCAL_DATE): for example 2016-10-30

d-MMM-yyyy pattern: for example 30-Oct-2016

d/MM/yyyy pattern: for example 30/10/2016

E, MMM d yyyy pattern: for example Sun, Oct 30 2016

EEEE, MMM d, yyyy HH:mm:ss a pattern: for example Sunday, Oct 30, 2016 12:10:56 PM

2016-08-16T15:23:01Z pattern(UTC time): for example 2016-08-16T15:23:01Z

Zone date time pattern: for example 2016-08-16T10:15:30+08:00

Convert a String into to LocalDateTime using below syntax:

Convert a String into a java.time.instant
Notice: a String is with the ‘Z’suffix is UTC time. we can convert it into a java.time.instant directly, then display it with a time zone.

Create a program for testing
We create main class to convert String to LocalDate Java 8 following all the above syntaxs

Run above main as Java application and produce the output like below:
How to convert String to LocalDate Java 8

That’s it on the tutorial How to convert String to LocalDate Java 8.

References
DateTimeFormatter JavaDoc
Convert String to date in Java
Wikipedia : ISO 8601
GMT VS UTC
What is a Time Zone?

Leave a Comment

*

Please share it if you found this useful
Hide Buttons