Date and Time

How to Convert String to Date in Java

Table of Contents

Program

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class StringToDate 
{
	public static void main(String[] args) throws Exception 
	{
		String d1 = "04/04/2021";
		System.out.println("String : " + d1);
		SimpleDateFormat sd1 = new SimpleDateFormat("dd/MM/yyyy");
		Date d = sd1.parse(d1);
		System.out.println("String To Date :" + d);
		
		
	}
}

Output

String : Tue Apr 13 12:15:49 IST 2021
String To Date : 2021-15-13 12:15:49

Description

public SimpleDateFormat()

Constructs a SimpleDateFormat using the default pattern and date format symbols
for the default FORMAT locale. Note: This constructor may not support all locales. For
full coverage, use the factory methods in the DateFormat class.

public final String format​(Object obj)


Formats an object to produce a string. This is equivalent to
format(obj, new StringBuffer(), new FieldPosition(0)).toString();

Parameters:

obj – The object to format

Returns:

Formatted string.

Wromn