1import java.text.SimpleDateFormat;
2import java.util.Date;
3public class StringToDateExample1 {
4public static void main(String[] args)throws Exception {
5 String sDate1="31/12/1998";
6 Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
7 System.out.println(sDate1+"\t"+date1);
8}
9}
1// How to convert string to date in Java 8
2import java.time.LocalDate;
3import java.time.LocalDateTime;
4public class Java8StringToDate
5{
6 public static void main(String[] args)
7 {
8 DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("d/MM/yyyy");
9 String strDate1 = "14/05/2005";
10 LocalDate ld1 = LocalDate.parse(strDate1, dtf1);
11 System.out.println(ld1);
12 DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM d yyyy");
13 String strDate2 = "May 14 2005";
14 LocalDate ld2 = LocalDate.parse(strDate2, dtf2);
15 System.out.println(ld2);
16 }
17}