-2

I have a string and i need to convert it to date in the "YYYY-mm-dd" format which i'm unable to do. I checked the similar post on stckoverflow but didnt help so i'm posting this query.

String stringDate = "Dec 13, 2013";

I need this to be convert to Date reportDate = 2013-12-13"; when i use simpledateformat i got unparsable date. Please help

madhu
  • 1,010
  • 5
  • 20
  • 38
  • This is not a duplicate... here the date format is DEC 13, 2013 not yyyy mm dd. pls give a solution here thanks – madhu Jan 10 '14 at 10:57
  • 1
    @madhu: You should be able to use that answer together with the Java docs to easily fix your problem as well. Did you try anything at all? – Keppil Jan 10 '14 at 10:58

5 Answers5

2

Check this out:

    try {
        SimpleDateFormat format1 = new SimpleDateFormat("MMM dd, yyyy");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        String stringDate = "Dec 13, 2013";

        Date date = format1.parse(stringDate);

        System.out.println(format2.format(date));
    } catch (ParseException exp) {
        // Take appropriate action
    }

Output: 2013-12-13

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
1

Use SimpleDateFormat as here: new_date is your Date

String stringDate = "Dec 13, 2013";
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD"); 
Date new_date=format.parse(stringDate);
cherry2891
  • 215
  • 1
  • 3
  • 13
1

In programming a very important skill is taking information that is close to what you need and using it to find what you do need.

Look at the link in the comments and you can see:

http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

Read that and you have what you need to adjust the answer there to give the results you want.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

I would do something like:

SimpleDateFormat parser = new SimpleDateFormat("MMM dd, yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date reportDate = parser.parse(stringDate)
String result = formatter.format(reportDate);
Raffaele Rossi
  • 1,079
  • 5
  • 11
0
String date="Your date";
SimpleDateFormat format = new SimpleDateFormat("MMM DD, YYYY"); 
Date d= format.parse(date);
DateFormat newFormat = new SimpleDateFormat("YYYY-MM-DD");
System.out.println(newFormat.format(d));
Helios
  • 851
  • 2
  • 7
  • 22