How to convert String to Date
807589Sep 21 2004 — edited Oct 7 2008Hello World,
I have date in String "mm/dd/yy" format I want to conver it into String format "yyyy-mm-dd".
I am doing this like
String MY_DATE_FORMAT = "yyyy-MM-dd";
Date d = new Date("05/18/05");
String expDate = new SimpleDateFormat(MY_DATE_FORMAT).format(d);
System.out.println(expDate);
Is there other good way to do this. Because the constructor of Date is depricated now.
I have tried parse() method of DateFormat but it is giving me exception.
I have tried something like this
try {
DateFormat df = new SimpleDateFormat();
Date d = df.parse("05/18/05");
String MY_DATE_FORMAT = "yyyy-MM-dd";
String expDate = new SimpleDateFormat(MY_DATE_FORMAT).format(d);
System.out.println(expDate);
} catch(Exception e) {
e.printStackTrace();
}
This code gives me exception :
java.text.ParseException: Unparseable date: "05/18/05"
at java.text.DateFormat.parse(DateFormat.java:345)
at StringTest.main(StringTest.java:19)
Experts, please help me in this as early as possible.
Regards,
Sachin Dare.