-2

I have a

String dateString = "Fri Feb 14 00:00:00 IST 2014";

I need output in Date datatype like 2014-02-14.

Here is the code which is throwing Parse exception.

Need help in this.

public static void main(String args[]){
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    String dateString = "Fri Feb 14 00:00:00 IST 2014";

        Date convertedDate = null;
        try {
            convertedDate = df.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(convertedDate);

}   
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
madhu
  • 1,010
  • 5
  • 20
  • 38
  • 1
    Obviously, `Fri Feb 14 00:00:00 IST 2014` does not match the `yyyy-MM-dd hh:mm:ss` format... See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for more information. – sp00m Feb 18 '14 at 12:07

3 Answers3

1

Base point is - Input string should match with date pattern

Raised parse exception as becasue wrong pattern, use this date pattern - EEE MMM dd hh:mm:ss z yyyy

DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

You have to convert dateString to matching Date format and then you can format that Date what ever the format you want.

Try this

  DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  DateFormat df2 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
  String dateString = "Fri Feb 14 00:00:00 IST 2014";
  Date date=df2.parse(dateString); // convert stringDate to matching Date format
  System.out.println(df1.format(date));

Out put:

  2014-02-14 12:00:00
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Try this:

public static void main(String[] args) {
    DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");

    String dateString = "Fri Feb 14 00:00:00 IST 2014";

    Date convertedDate = null;
    try {
        convertedDate = df.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(convertedDate);
}
Salah
  • 8,567
  • 3
  • 26
  • 43