The pattern you are using for your SimpleDateFormat
is wrong.
This pattern should match your given date format:
EEE MMM dd HH:mm:ss Z yyyy
Use it when you create your SimpleDateFormat
:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
For more info on what the different letters mean, have a look at the documentation for SimpleDateFormat
.
Note also that SimpleDateFormat.parse()
won't give you a java.sql.Date
as mentioned in the title, but rather a java.util.Date
.
Edit: To output the date in a different format, you would create a second SimpleDateFormat
with the pattern that represents the desired output format. Then you can convert it back to the String you want using SimpleDateFormat.format()
:
SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
String output = outputFormat.format(yourDate);
System.out.println(output);
All in all, this results in the following code:
String input = "Wed Jan 01 00:00:00 CST 2014";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try {
Date date = inputFormat.parse(input);
SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
String output = outputFormat.format(date);
System.out.println(output); // 01/01/2014
} catch (ParseException e) {
e.printStackTrace();
}