-3

All,

I need to convert the following string "Tue Jan 01 08:00:00 CET 2013" to a date object with format "dd/MM/yyyy HH:mm".

What I have done till now...

    String dateStr = "Tue Jan 01 08:00:00 CET 2013";
    DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");

    DateFormat writeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    Date date = null;
    try 
    {
       date = readFormat.parse(dateStr);

    } catch ( ParseException e ) 
    {
        e.printStackTrace();
    }

    String formattedDate = "";
    if( date != null ) 
    {
        formattedDate = writeFormat.format(date);
    }

    System.out.println(formattedDate);

But this gives me a String as a result and not a date. If I parse the formattedDate String again using the writeFormat then I get the same original date back again i.e. Tue Jan 01 08:00:00 CET 2013.

NOTE: Finally, I want to push the date into MySQL DateTime datatype via Java Date object. i.e. String -> Java Date -> MySQL Date Time.

I have searched High/Low on the web and could not find a proper solution. Please help!!!

Thanks and regards,

SG

Renjith Krishnan
  • 2,446
  • 5
  • 29
  • 53
SSG
  • 192
  • 2
  • 12

1 Answers1

0

I found the answer.

DateFormat readFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
date = readFormat.parse(string);

And then while saving to MYSQL database...

pstmt.setDate(5, new java.sql.Date(change.getEndDateTime().getTime()));

Thanks for all your responses and apologies for posting duplicate if it is...

Regards, SG

SSG
  • 192
  • 2
  • 12