-3

I have a date with the format as Fri Nov 16 00:00:00 CXT 2012

And I want to store in Mysql database using my java program.

Im writing the following code

    SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
    java.util.Date date = sdf1.parse(split[4]);
    java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());

But it throws this error Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Nov 16 00:00:00 CXT 2012"

How to do it. ?

Rahuul
  • 31
  • 1
  • 9
  • 2
    Obviously, you have to start coding. Hint : SimpleDateFormat class in java. – Suresh Atta Jul 29 '14 at 08:04
  • Where's your problem? Formatting/parsing the date? Writing to the database? What have you tried already? – Thomas Jul 29 '14 at 08:05
  • Hi thomas , I have edited my post with code and error – Rahuul Jul 29 '14 at 08:08
  • 1
    so why do you think that `Fri Nov 16 00:00:00 ` will parse as `dd-MM-yyyy` ? – Scary Wombat Jul 29 '14 at 08:09
  • The format you provided obviously doesn't match the date, as Scary Wombat already said. You'd need to change the format to include the day-of-week, the month name in abreviated form, the time as well as the time zone and the year moved to the end of the date. – Thomas Jul 29 '14 at 08:16

1 Answers1

2

This is exactly whay you need:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Solution {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
        Date parsed = format.parse("Fri Nov 16 00:00:00 CXT 2012");
        java.sql.Date sql = new java.sql.Date(parsed.getTime());
    }
}
Balduz
  • 3,560
  • 19
  • 35