-5

Possible Duplicate:
Conversion of Date
How to convert date from m/d/yy to mm/dd/yyyy?

I get the date in a String as: 5/1/12. I need to convert it to 05/01/2012. Please suggest.

I used the following code:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

d_date = dateFormat.parse("5/1/12"); 

strDate = dateFormat.format(d_date);

Result:
05/01/0012

Expected Result: 05/01/2012

Thanks Prasad

Community
  • 1
  • 1
Prasad Kothamasu
  • 25
  • 1
  • 1
  • 3
  • 4
    removed `javaScript` tag. It's wise to read comments when your question is closed to learn from mistakes: http://stackoverflow.com/questions/10404824/how-to-convert-date-from-m-d-yy-to-mm-dd-yyyy – nuala May 01 '12 at 22:09
  • 2
    Why would you tag Javascript and Java? They're two completely different things. – anonymous May 01 '12 at 22:10
  • *Please* don't re-post questions. Fix the original post, and try to get it re-opened. – Kendall Frey May 01 '12 at 22:10
  • 2
    -1 You have repeated the same question once again even though the previous one had already been closed. This is considered to be the abuse of the community on SO. – Lion May 01 '12 at 22:11
  • http://stackoverflow.com/questions/473282/left-padding-integers-with-zeros-in-java (from the previous closed question) – Andrew Leach May 01 '12 at 22:11
  • It says marked as duplicate but even original is removed – Ujju Sep 30 '15 at 14:39

1 Answers1

5

Try the following:

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");

formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);

The following links should prove helpful for more complicated tasks:

Oracle SimpleDateFormat Docs

Example code

ashurexm
  • 6,209
  • 3
  • 45
  • 69