Java Program to Convert String to Date

Write a Java program to convert string to date with an example. In Java, we can use the LocalDate parse method to convert string to date.

package NumPrograms;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class StringToDate1 {

	public static void main(String[] args) {
		
		String str1 = "2019-05-30";
		LocalDate dt1 = LocalDate.parse(str1);
		LocalDate dt2 = LocalDate.parse(str1, DateTimeFormatter.ISO_DATE);
		
		System.out.println("After Converting String to Date");
		System.out.println(dt1);
		System.out.println(dt2);
	}
}
Java Program to Convert String to Date

This Java Program uses the LocalDate and DateTimeFormatter to convert String to Date. If the string date is not in the standard format, we can use the DateTimeFormatter to get the pattern. Next, we can pass that formatter to LocalDate. 

package NumPrograms;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class StringToDate2 {

	public static void main(String[] args) {
		
		String str1 = "10-04-2017";
		DateTimeFormatter form1 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
		LocalDate dt1 = LocalDate.parse(str1, form1);
		
		String str2 = "May 12, 2019";
		DateTimeFormatter form2 = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
		LocalDate dt2 = LocalDate.parse(str2, form2);
		
		String str3 = "2018, June 19 11:05:33";
		DateTimeFormatter form3 = DateTimeFormatter.ofPattern("yyyy, MMMM d hh:mm:ss");
		LocalDate dt3 = LocalDate.parse(str3, form3);
		
		System.out.println("After Converting String to Date");
		System.out.println(dt1);
		System.out.println(dt2);
		System.out.println(dt3);
	}
}
After Converting String to Date
2017-04-10
2019-05-12
2018-06-19

Java Program to Convert String to Date using SimpleDateFormat parse method

package NumPrograms;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate3 {

	public static void main(String[] args) throws Exception {
		
		String str1 = "22/05/2019";
		Date dt1 = new SimpleDateFormat("dd/MM/yyyy").parse(str1);
		
		String str2 = "11/12/2019";
		Date dt2 = new SimpleDateFormat("MM/dd/yyyy").parse(str2);
		
		System.out.println("After Converting String to Date");
		System.out.println(dt1);
		System.out.println(dt2);
	}
}
After Converting String to Date
Wed May 22 00:00:00 IST 2019
Tue Nov 12 00:00:00 IST 2019

In this Java example, we defined dates in different Pattern Formatters and used SimpleDateFormat parse to convert string to date.

package NumPrograms;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate4 {

	public static void main(String[] args) throws Exception {
		
		String str1 = "10-11-2011";
		Date dt1 = new SimpleDateFormat("dd-MM-yyyy").parse(str1);
		
		String str2 = "31 12, 2012";
		Date dt2 = new SimpleDateFormat("dd MM, yyyy").parse(str2);
		
		String str3 = "Mon, Dec 31, 2013";
		Date dt3 = new SimpleDateFormat("E, MMM dd, yyyy").parse(str3);
		
		String str4 = "Mon, Dec 31, 2013 23:59:58";
		Date dt4 = new SimpleDateFormat("E, MMM dd, yyyy hh:mm:ss").parse(str4);
		
		System.out.println("After Converting String to Date");
		System.out.println(dt1);
		System.out.println(dt2);
		System.out.println(dt3);
		System.out.println(dt4);
	}
}
After Converting String to Date
Thu Nov 10 00:00:00 IST 2011
Mon Dec 31 00:00:00 IST 2012
Tue Dec 31 00:00:00 IST 2013
Tue Dec 31 23:59:58 IST 2013