java tutorial - How to convert String to Date in Java - java programming - learn java - java basics - java for beginners
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.
Learn java - java tutorial - java string to date - java examples - java programs
Syntax
// String -> Date
SimpleDateFormat.parse(String);
| LETTER | DESCRIPTION | EXAMPLES |
|---|---|---|
| y | Year | 2018 |
| M | Month in year | July, 07, 7 |
| d | Day in month | 1-31 |
| E | Day name in week | Friday, Sunday |
| a | Am/pm marker | AM, PM |
| H | Hour in day | 0-23 |
| h | Hour in am/pm | 1-12 |
| m | Minute in hour | 0-60 |
| s | Second in minute | 0-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