convert string date to date code example
Example 1: convert string to localdatetime
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Example 2: string to date conversion java
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}
}
Example 3: How to convert string to date in Java 8
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Java8StringToDate
{
public static void main(String[] args)
{
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("d/MM/yyyy");
String strDate1 = "14/05/2005";
LocalDate ld1 = LocalDate.parse(strDate1, dtf1);
System.out.println(ld1);
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM d yyyy");
String strDate2 = "May 14 2005";
LocalDate ld2 = LocalDate.parse(strDate2, dtf2);
System.out.println(ld2);
}
}
Example 4: how to convert string to date
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}
}
Example 5: from string to date
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}