Convert String to LocalDate in Java

Problem

Given the multiple following String containing a textual representation of a LocalDate:

String s1 = "2019-12-31";
String s2 = "31-12-19";
String s3 = "31-12-2019";
String s4 = "31/12/2019";
String s5 = "12/31/2019";
String s6 = "2019/12/31";

How to convert these String to a LocalDate?

Solution

LocalDate d1 = LocalDate.parse(s1);
LocalDate d2 = LocalDate.parse(s2, DateTimeFormatter.ofPattern("dd-MM-yy"));
LocalDate d3 = LocalDate.parse(s3, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
LocalDate d4 = LocalDate.parse(s4, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
LocalDate d5 = LocalDate.parse(s5, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
LocalDate d6 = LocalDate.parse(s6, DateTimeFormatter.ofPattern("yyyy/MM/dd"));