Java 8 – How to convert String to LocalDate

Few Java examples show you how to convert a String to the new Java 8 Date API – java.time.LocalDate


	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

	String date = "16/08/2016";
	
	//convert String to LocalDate
	LocalDate localDate = LocalDate.parse(date, formatter);
Note
Refer to this official DateTimeFormatter JavaDoc for more date time formatter examples.
Note
You may interest at this classic java.util.Date example – How to convert String to Date in Java

1. String = 2016-08-16

If the String is formatted like ISO_LOCAL_DATE, you can parse the String directly, no need conversion.

TestNewDate1.java

package com.mkyong.java8.date;

import java.time.LocalDate;

public class TestNewDate1 {

    public static void main(String[] argv) {

        String date = "2016-08-16";

		//default, ISO_LOCAL_DATE
        LocalDate localDate = LocalDate.parse(date);

        System.out.println(localDate);

    }

}

Output


2016-08-16

2. String = 16-Aug-2016

TestNewDate2.java

package com.mkyong.java8.date;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class TestNewDate2 {

    public static void main(String[] argv) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy");
        
		String date = "16-Aug-2016";

        LocalDate localDate = LocalDate.parse(date, formatter);
 
        System.out.println(localDate);  //default, print ISO_LOCAL_DATE

        System.out.println(formatter.format(localDate));

    }

}

Output


2016-08-16
16-Aug-2016

3. String = 16/08/2016

TestNewDate3.java

package com.mkyong.java8.date;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class TestNewDate3 {

    public static void main(String[] argv) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

        String date = "16/08/2016";

        LocalDate localDate = LocalDate.parse(date, formatter);

        System.out.println(localDate);

        System.out.println(formatter.format(localDate));

    }

}

Output


2016-08-16
16/08/2016

4. String = Tue, Aug 16 2016

TestNewDate4.java

package com.mkyong.java8.date;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class TestNewDate4 {

    public static void main(String[] argv) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy");

        String date = "Tue, Aug 16 2016";

        LocalDate localDate = LocalDate.parse(date, formatter);

        System.out.println(localDate);

        System.out.println(formatter.format(localDate));

    }

}

Output


2016-08-16
Tue, Aug 16 2016

5. String = Tuesday, Aug 16, 2016 12:10:56 PM

This example convert a String to java.time.LocalDateTime

TestNewDate5.java

package com.mkyong.java8.date;

package com.mkyong.pageview;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TestNewDate5 {

    public static void main(String[] argv) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a");

        String date = "Tuesday, Aug 16, 2016 12:10:56 PM";

        LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);

        System.out.println(localDateTime);

        System.out.println(formatter.format(localDateTime));

    }

}

Output


2016-08-16T12:10:56
Tuesday, Aug 16, 2016 12:10:56 PM

6. String = 2016-08-16T15:23:01Z

The ‘Z’ suffix means UTC, you can convert into a java.time.instant directly, then display it with a time zone.

TestNewDate6.java

package com.mkyong.java8.date;

import java.time.*;

public class TestNewDate6 {

    public static void main(String[] argv) {

        String dateInString = "2016-08-16T15:23:01Z";

        Instant instant = Instant.parse(dateInString);

        System.out.println("Instant : " + instant);

        //get date time only
        LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId()));

        //get localdate
        System.out.println("LocalDate : " + result.toLocalDate());

        //get date time + timezone
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Tokyo"));
        System.out.println(zonedDateTime);

        //get date time + timezone
        ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/Athens"));
        System.out.println(zonedDateTime2);

    }

}

Output


Instant : 2016-08-16T15:23:01Z
LocalDate : 2016-08-16
2016-08-17T00:23:01+09:00[Asia/Tokyo]
2016-08-16T18:23:01+03:00[Europe/Athens]

7. String = 2016-08-16T10:15:30+08:00

String -> ZonedDateTime -> LocalDate

TestNewDate7.java

package com.mkyong.java8.date;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class TestNewDate7 {

    public static void main(String[] argv) {

        String date = "2016-08-16T10:15:30+08:00";

        ZonedDateTime result = ZonedDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);

        System.out.println("ZonedDateTime : " + result);

        System.out.println("TimeZone : " + result.getZone());

        LocalDate localDate = result.toLocalDate();

        System.out.println("LocalDate : " + localDate);

    }

}

Output


ZonedDateTime : 2016-08-16T10:15:30+08:00
TimeZone : +08:00
LocalDate : 2016-08-16

References

  1. DateTimeFormatter JavaDoc
  2. Classic SimpleDateFormat JavaDoc
  3. Java – How to convert String to Date
  4. Stackoverflow : simpledateformat parsing date with ‘Z’ literal
  5. Wikipedia : ISO 8601
  6. GMT VS UTC
  7. What is a Time Zone?

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Mick
Guest
Mick

Add the locale to your examples, otherwise you might get a parse error

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“EEEE, MMM d, yyyy HH:mm:ss a”); // no locale
String date = “Tuesday, Aug 16, 2016 12:10:56 PM”; // error for “Tuesday” if you are not in the US

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“EEEE, MMM d, yyyy HH:mm:ss a”, Locale.US);
String date = “Tuesday, Aug 16, 2016 12:10:56 PM”; // no error, everything´s fine

Reza
Guest
Reza

you must use
DateTimeFormatter DTF_DATETIME = DateTimeFormatter.ofPattern(“dd-MMM-yyyy”, Locale.ENGLISH);
instead of DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“d-MMM-yyyy”);

Eugene
Guest
Eugene

Hello, tell me please, how parse date with the format – “dd MMM” (02 Jan)?

Thanks.. :-)

Reza
Guest
Reza

java.time.format.DateTimeParseException: Text ’16-Aug-2016′ could not be parsed at index 3
It’s wrong… just waste of time by using your site !!!

sbruble
Guest
sbruble

That’s because “Aug” is in English, so you must set a java.util.Locale in the formatter:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“d-MMM-yyyy”, Locale.US);

The code without the locale will work only if the JVM default locale is english.

That’s why I don’t like this site, the examples are very innacurate (don’t work all the times, just for the author’s default configs), without any explanation about why the code works (which is much more important than just “here’s the code, just copy, paste and don’t think about it”) and so on.

anurag
Guest
anurag

nice article rally very nice .
thank you so much
keep posting for us.

Zoltán
Guest
Zoltán

I have been told that instead of
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“d/MM/yyyy”);
String date = “16/08/2016”;
LocalDate localDate = LocalDate.parse(date, formatter);
I should use
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“d/MM/yyyy”);
String date = “16/08/2016”;
LocalDate localDate = formatter.parse(date);

sbruble
Guest
sbruble

formatter.parse returns a TemporalAccessor, while LocalDate.parse returns the correct type (LocalDate).
Although you can do formatter.parse(date, LocalDate::from), which will also return a LocalDate

bliako
Guest
bliako

You left the most important bit out: How do you check for failure in parsing a date?????????