import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class TimeString {
public static void main(String[] args) {
// Format y-M-d or yyyy-MM-d
String string = "2018-05-16";
LocalDate date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE);
System.out.println(date);
}
}2018-05-16 2018-05-16 or 2018-05-16+05:45'.parse() function parses the given string using the given formatter. You can also remove the ISO_DATE formatter in the above example and replace the parse() method with:LocalDate date = LocalDate.parse(string, DateTimeFormatter);
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class TimeString {
public static void main(String[] args) {
String string = "May 16, 2018";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date);
}
}2018-05-16MMMM d, yyyy. So, we create a formatter of the given pattern. LocalDate.parse() function and get the LocalDate object.