Java 8 - convert String to LocalDate
Short solution:
// solution 1
String strDate = "24/12/2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse(strDate, formatter); // 2019-12-24
Below we have examples showing how we can convert string date in different formats to LocalDate. LocalDate class was introduced in Java 8.
1. Using DateTimeFormatter and LocalDate.parse()
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Example1 {
public static void main(String[] args) {
String strDate = "24/12/2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse(strDate, formatter);
System.out.println(localDate); // 2019-12-24
}
}
2. Format - yyyy-mm-dd
String to LocalDate with date format yyyy-mm-dd - ISO_LOCAL_DATE
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample1_ISO_LOCAL_DATE {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
String date = "2019-10-12";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(date);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
2019-10-12
2019-10-12
3. Format - yyyymmdd
String to LocalDate with date format yyyymmdd - BASIC_ISO_DATE
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample2_BASIC_ISO_DATE {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
String date = "20191012";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
20191012
4. Format - E, MMM dd yyyy
String to LocalDate with date format E, MMM dd yyyy
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample3 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy");
String date = "Sat, Oct 12 2019";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
Sat, Oct 12 2019
5. Format - EEEE, MMM dd yyyy
String to LocalDate with date format EEEE, MMM dd yyyy
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample4 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy");
String date = "Saturday, Oct 12 2019";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
Saturday, Oct 12 2019
6. Format - yyyy/MM/dd
String to LocalDate with date format yyyy/MM/dd
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample5 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String date = "2019/10/12";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
2019/10/12
7. Format - yyyy-MM-dd
String to LocalDate with date format yyyy-MM-dd
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample6 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date = "2019-10-12";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
2019-10-12
8. Format - yyyy-MM-dd
String to LocalDate with date format yyyy-MM-dd - DEFAULT - ISO_LOCAL_DATE
import java.time.LocalDate;
public class LocalDateExample7_DEFAULT {
public static void main(String[] args) {
// default - parsed using DateTimeFormatter.ISO_LOCAL_DATE
// 2019-10-12
String date = "2019-10-12";
LocalDate localDate = LocalDate.parse(date);
System.out.println("String date: ");
System.out.println(date);
System.out.println("LocalDate: ");
System.out.println(localDate);
}
}
Output:
String date:
2019-10-12
LocalDate:
2019-10-12
9. Format - yyyy-MM-dd
String to LocalDate with date format yyyy-MM-dd - ISO_DATE
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample8_ISO_DATE {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
String date = "2019-10-12";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
2019-10-12
10. Format - yyyy-MM-dd+hh:mm
String to LocalDate with date format yyyy-MM-dd+hh:mm - ISO_DATE with time zone
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample9_ISO_DATE_WITH_ZONE {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
String date = "2019-10-12+01:00";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
System.out.println(formatter.format(localDate));
}
}
Output:
2019-10-12
2019-10-12
11. String to LocalDate with 9 examples
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ConvertStringToLocalDateExamples {
public static void main(String[] args) {
System.out.println("# Example 1");
// default - parsed using DateTimeFormatter.ISO_LOCAL_DATE
// 2019-10-12
System.out.println(LocalDate.parse("2019-10-12"));
System.out.println("# Example 2");
// 2019-10-12
System.out.println(LocalDate.parse("2019-10-12",
DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println("# Example 3");
// 2019-10-12
System.out.println(LocalDate.parse("20191012",
DateTimeFormatter.BASIC_ISO_DATE));
System.out.println("# Example 4");
// 2019-10-12
System.out.println(LocalDate.parse("Sat, Oct 12 2019",
DateTimeFormatter.ofPattern("E, MMM dd yyyy")));
System.out.println("# Example 5");
// 2019-10-12
System.out.println(LocalDate.parse("Saturday, Oct 12 2019",
DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy")));
System.out.println("# Example 6");
// 2019-10-12
System.out.println(LocalDate.parse("2019/10/12",
DateTimeFormatter.ofPattern("yyyy/MM/dd")));
System.out.println("# Example 7");
// 2019-10-12
System.out.println(LocalDate.parse("2019-10-12",
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
System.out.println("# Example 8");
// 2019-10-12
System.out.println(LocalDate.parse("2019-10-12",
DateTimeFormatter.ISO_DATE));
System.out.println("# Example 9");
// 2019-10-12
System.out.println(LocalDate.parse("2019-10-12+01:00",
DateTimeFormatter.ISO_DATE));
}
}
Output:
# Example 1
2019-10-12
# Example 2
2019-10-12
# Example 3
2019-10-12
# Example 4
2019-10-12
# Example 5
2019-10-12
# Example 6
2019-10-12
# Example 7
2019-10-12
# Example 8
2019-10-12
# Example 9
2019-10-12