Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to convert String to Date

807589Sep 21 2004 — edited Oct 7 2008
Hello 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2008
Added on Sep 21 2004
39 comments
1,376 views