Java Program to Convert String to Date

Write a Java program to convert string to date with an example. There are many options to achieve it, including importing the LocalDate class and SimpleDateFormat. Apart from them, you can import the DateTimeFormatter and use it with the LocalDate class parse() function to change the formatting.

Java Program to Convert String to Date

In this example, we used the LocalDate parse() method to convert string to date. First, we declared a string variable of str1 and assigned a random date. Next, we created the instance of the LocalDate class and used the parse() function associated with it.

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 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 Example2 {

	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(dt1);
		System.out.println(dt2);
		System.out.println(dt3);
	}
}
2017-04-10
2019-05-12
2018-06-19

Java Program to Convert String to Date using SimpleDateFormat parse function

Importing the SimpleDateFormat and using it is one of the effective ways of formatting dates. First, create an instance of the SimpleDateFormat and then use the parse() method. Don’t forget to mention the date format you will parse. For instance, dd/MM/yyyy or yyyy/MM/dd.

package NumPrograms;

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

public class Example3 {

	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(dt1);
		System.out.println(dt2);
	}
}
Wed May 22 00:00:00 IST 2019
Tue Nov 12 00:00:00 IST 2019

In this 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(dt1);
		System.out.println(dt2);
		System.out.println(dt3);
		System.out.println(dt4);
	}
}
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