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.Dateexample – How to convert String to Date in Java
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.favtuts.time;
import java.time.LocalDate;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringISOLOCALDATEformat();
}
static void parseStringISOLOCALDATEformat() {
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.English
JavaDateExample2.java
package com.favtuts.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringLocaleEnglish();
}
static void parseStringLocaleEnglish() {
//running fine only if the default locale understands English
System.out.println(Locale.getDefault()); // en_US
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.favtuts.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringFailedWithLocaleFrance();
}
static void parseStringFailedWithLocaleFrance() {
// 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); //default, print ISO_LOCAL_DATE
System.out.println(formatter.format(localDate)); // print formatter date
}
}
Output
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)
at com.favtuts.time.StringToLocalDateExamples.parseStringFailedWithLocaleFrance(StringToLocalDateExamples.java:25)
at com.favtuts.time.StringToLocalDateExamples.main(StringToLocalDateExamples.java:12
2.3 The safest way is always specified a Locale.US for DateTimeFormatter.
JavaDateExample2.java
package com.favtuts.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringSuccessWithLocaleFrance();
}
static void parseStringSuccessWithLocaleFrance() {
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.favtuts.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringDMMYYYYformat();
}
static void parseStringDMMYYYYformat() {
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.favtuts.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringEMMMdyyyyFormat();
}
static void parseStringEMMMdyyyyFormat() {
// 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.LocalDateTime
JavaDateExample5.java
package com.favtuts.time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringEEEEMMMdyyyyhhmmssa();
}
static void parseStringEEEEMMMdyyyyhhmmssa() {
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.favtuts.time;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringUTCwithInstantZonedDateTime();
}
static void parseStringUTCwithInstantZonedDateTime() {
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.favtuts.time;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringISODATETIMEwithZonedDateTime();
}
static void parseStringISODATETIMEwithZonedDateTime() {
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.favtuts.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
public class StringToLocalDateExamples {
public static void main(String[] args) {
parseStringThrowDateTimeParseException();
}
static void parseStringThrowDateTimeParseException() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-MMM-yyyy", Locale.US);
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!
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-basic/time