java tutorial - How to convert String to Date in Java - java programming - learn java - java basics - java for beginners



 String to Date in Java

Learn Java - Java tutorial - String to Date in Java - Java examples - Java programs

String to Date in Java

  • We can convert String to Date in java using parse() method of DateFormat as well as SimpleDateFormat classes.
 java string to date

Learn java - java tutorial - java string to date - java examples - java programs

Syntax

// String -> Date
SimpleDateFormat.parse(String);
LETTER DESCRIPTION EXAMPLES
yYear2018
MMonth in yearJuly, 07, 7
dDay in month1-31
EDay name in weekFriday, Sunday
aAm/pm markerAM, PM
HHour in day0-23
hHour in am/pm1-12
mMinute in hour0-60
sSecond in minute0-60

Example

import java.text.SimpleDateFormat;  
import java.util.Date;  
public class StringToDateExample1 {  
public static void main(String[] args)throws Exception {  
    String sDate1="08/02/2018";  
    Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);  
    System.out.println(sDate1+"\t"+date1);  
}  
}  

Output:

08/02/2018 Thu Feb 08 00:00:00 IST 2018

Related Searches to How to convert String to Date in Java