-1
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");    
String stringdate = "Wed Jan 01 00:00:00 CST 2014";
Date stringFromDate = null;
try {
  Date dtt= formatter.parse(stringDate);
  System.out.println(dtt);
} 
catch (ParseException e1) {
  e1.printStackTrace();
}

I am receiving an exception java.text.ParseException: Unparseable date: "Wed Jan 01 00:00:00 CST 2014" tried so many times but it is not working.

Here i have "stringdate " as fixed format and i have to display in MM/DD/yyyy format. That's my requirement.

tom
  • 5,114
  • 6
  • 24
  • 36
  • 2
    The format of your date doesn't match with the format used in the `SimpleDateFormat`. – Luiggi Mendoza May 20 '15 at 21:10
  • unable to find the match can you please help on that. – tom May 20 '15 at 21:10
  • Not sure what you mean by "unable to find the match" - have you read the documentation for `SimpleDateFormat`? Compare your pattern with the value you're parsing... – Jon Skeet May 20 '15 at 21:11
  • Read the [API documentation of `SimpleDateFormat`](http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) (<= click the link), it explains exactly what the pattern means. – Jesper May 20 '15 at 21:12
  • If you check your current format, you're using MM/dd/yyy which means 2 digits for month, followed by `/`, followed by two digits for day, followed by `/`, followed by 4 digits for year, and you're sending `"Wed Jan 01"` (apart from the rest of the format and from your String). How do you expect a match? – Luiggi Mendoza May 20 '15 at 21:12
  • i found the patter to be "EEE MMM dd hh:mm:ss z yyyy" – tom May 20 '15 at 21:16
  • But i am expecting the date to be in "MM/DD/yyyy" – tom May 20 '15 at 21:17
  • possible duplicate of [SimpleDateFormat](http://stackoverflow.com/questions/3056703/simpledateformat) – zEro May 20 '15 at 21:18
  • @tom What exactly do you mean? `Date` objects do not have a format. It's not possible to have a `Date` object in a certain format. A `Date` object is just a date value, it doesn't have a format by itself. – Jesper May 20 '15 at 21:18
  • You probably mean `MM/dd/yyyy` instead of `MM/DD/yyyy` since this would result in an interesting output. `D` stands for *Day in Year*. – TimoStaudinger May 20 '15 at 21:30

2 Answers2

2

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();
}
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • Its the other way, I have the String format of date fixed. I want to change it to MM/dd/yyyy – tom May 20 '15 at 21:16
  • 1
    So, you have a fixed pattern you create your `SimpleDateFormat` with and you can create your `stringdate` the way you want? Or the other way round? – TimoStaudinger May 20 '15 at 21:17
  • exactly, i want to display the date to be in MM/DD/YYYY format – tom May 20 '15 at 21:18
1

To get Date in MM/dd/yyyy try using second SimpleDateFormat as:

new SimpleDateFormat("MM/dd/yyyy");
hitz
  • 1,100
  • 8
  • 12