Java 8 Parse String to Date

Let’s see how you can convert from string to date in java 8.

1) Convert string to date in ISO8601 format

By default, java dates are in ISO8601 format, so if you have any string which represent date in ISO8601 format then you can use LocalDate.parse() or LocalDateTime.parse() methods directly.

String armisticeDate = "2016-04-04";

LocalDate aLD = LocalDate.parse(armisticeDate);
System.out.println("Date: " + aLD);

String armisticeDateTime = "2016-04-04T11:50";

LocalDateTime aLDT = LocalDateTime.parse(armisticeDateTime);
System.out.println("Date/Time: " + aLDT);


Date: 2016-04-04
Date/Time: 2016-04-04T11:50

2) Convert string to date in custom formats

If you have dates in some custom format, then you need to put additional logic to handle formatting as well using DateTimeFormatter.ofPattern().

String anotherDate = "04 Apr 2016";

DateTimeFormatter df = DateTimeFormatter.ofPattern("dd MMM yyyy");
LocalDate random = LocalDate.parse(anotherDate, df);

System.out.println(anotherDate + " parses as " + random);

Happy Learning !!



Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial