Java 8 – How to convert String to LocalDate?

Here are a few Java examples of converting 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);

The key is understand the DateTimeFormatter patterns

Note
You may interest at this classic java.util.Date 

1. 2016-08-16

If the String is in ISO_LOCAL_DATE format, we can parse the String directly, no need conversion.JavaDateExample1.java

package com.mkyong.date;

import java.time.LocalDate;

public class JavaDateExample1 {

    public static void main(String[] args) {

        String date = "2016-08-16";

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

        System.out.println(localDate);

    }
}

Output

2016-08-16

2. 16-Aug-2016 + Locale.US

2.1 The below program is running fine only if the default locale understands English. For example, Locale.US or Locale.EnglishJavaDateExample2.java

package com.mkyong.date;

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

public class JavaDateExample2 {

    public static void main(String[] args) {

        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)); // print formatter date

    }
}

Output

2016-08-16
16-Aug-2016

2.2 Now, change the locale to Locale.FRANCE, the DateTimeFormatter will fail to parse the Aug and throws DateTimeParseException exception:JavaDateExample2.java

package com.mkyong.date;

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

public class JavaDateExample2 {

    public static void main(String[] args) {

        // not all default locale is Locale.US
        // simulate a France locale.
        Locale.setDefault(Locale.FRANCE);

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

        String date = "16-Aug-2016";

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

        System.out.println(localDate);

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

    }
}
Exception in thread "main" java.time.format.DateTimeParseException: Text '16-Aug-2016' could not be parsed at index 3
 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
 at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
 at java.base/java.time.LocalDate.parse(LocalDate.java:428)

2.3 The safest way is always specified a Locale.US for DateTimeFormatter.JavaDateExample2.java

package com.mkyong.date;

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

public class JavaDateExample2 {

    public static void main(String[] args) {

        Locale.setDefault(Locale.FRANCE);

        // no problem now, DateTimeFormatter always uses Locale.US
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US);

        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)); // print formatted date

    }
}

Output

2016-08-16
16-Aug-2016

3. 16/08/2016

JavaDateExample3.java

package com.mkyong.date;

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

public class JavaDateExample3 {

    public static void main(String[] args) {

        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. Tue, Aug 16 2016

JavaDateExample4.java

package com.mkyong.date;

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

public class JavaDateExample4 {

    public static void main(String[] args) {

        // define a locale which understand English words, refer example 2 bug
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM d yyyy", Locale.US);

        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. Tuesday, Aug 16, 2016 12:10:56 PM

This example convert a String to java.time.LocalDateTimeJavaDateExample5.java

package com.mkyong.date;

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

public class JavaDateExample5 {

    public static void main(String[] args) {

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

        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. 2016-08-16T15:23:01Z

The Z suffix means UTC, convert the String to java.time.instant first, and play around the time with ZonedDateTime.JavaDateExample6.java

package com.mkyong.date;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class JavaDateExample6 {

    public static void main(String[] args) {

        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. 2016-08-16T10:15:30+08:00

The last +08:00 is a timezone. String -> ZonedDateTime -> LocalDate.JavaDateExample7.java

package com.mkyong.date;

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

public class JavaDateExample7 {

    public static void main(String[] args) {

        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

8. DateTimeParseException

If the date is unable to parse, it will throw DateTimeParseException.JavaDateExample8.java

package com.mkyong.demo;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;

public class JavaDateExample8 {

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US);

    public static void main(String[] args) {

        try {
            LocalDate localDate = LocalDate.parse("16-ABC-2016", dtf);
            System.out.println(dtf.format(localDate));
        } catch (DateTimeParseException e) {
            System.err.println("Unable to parse the date!");
            //e.printStackTrace();
        }

    }

}

Output

Unable to parse the date!

References

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *