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:
1 2 3 4 |
String format = "dd-MMM-yyyy"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); String date = "30-Oct-2016"; LocalDate localDate = LocalDate.parse(date, formatter); |
if your string is formatted like ISO_LOCAL_DATE (format=yyyy-MM-dd), you can parse the String directly without the help of DateTimeFormatter.
1 2 |
String date = "2016-10-30"; LocalDate localDate = LocalDate.parse(date); //default format is ISO_LOCAL_DATE |
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:
1 2 3 4 |
String format = "..."; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); String date = "..."; LocalDateTime localDateTime = LocalDateTime.parse(date, formatter); |
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.
1 2 3 4 5 6 7 |
String dateInString = "..."; // for example: 2016-08-16T15:23:01Z Instant instant = Instant.parse(dateInString); //get date time only LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId())); //get date time with timezone ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("America/Los_Angeles")); |
Create a program for testing
We create main class to convert String to LocalDate Java 8 following all the above syntaxs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.javabycode.date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class ConvertStringtoDateInJava8 { public static void main(String args[]){ //for example: ISO_LOCAL_DATE format String date = "2016-10-30"; LocalDate localDate = LocalDate.parse(date); System.out.println("ISO_LOCAL_DATE format = "+localDate); //for example: d-MMM-yyyy format String format2 = "d-MMM-yyyy"; DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(format2); String date2 = "30-Oct-2016"; LocalDate localDate2 = LocalDate.parse(date2, formatter2); System.out.println(format2+" format = "+localDate2); //for example: LocalDateTime String format3 = "EEEE, MMM d, yyyy HH:mm:ss a"; DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("EEEE, MMM d, yyyy HH:mm:ss a"); String date3 = "Sunday, Oct 30, 2016 12:10:56 PM"; LocalDateTime localDateTime3 = LocalDateTime.parse(date3, formatter3); System.out.println(format3+" format = "+localDateTime3); //for example: UTC time format String date4 = "2016-10-30T15:23:01Z"; Instant instant = Instant.parse(date4); System.out.println("Instant : " + instant); //get date time only LocalDateTime localDateTime4 = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId())); //get localdate System.out.println("LocalDate : " + localDateTime4.toLocalDate()); //get date time + timezone ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Tokyo")); System.out.println("zonedDateTime = " +zonedDateTime); } } |
Run above main as Java application and produce the output like below:
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?