Java DateTimeFormatter parse String like "31/10/2020" to LocalDate

Description

Java DateTimeFormatter parse String like "31/10/2020" to LocalDate


import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Main {
    public static void main(String[] args) {
        String stringDate = "31/10/2020";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate localDate = LocalDate.parse(stringDate, formatter);
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
    }/* w ww . j  a va 2  s.  c  om*/
}



PreviousNext

Related