You are instructing the date parser that your input strdate
is in format dd-MMM-yyyy
. But actually it is in EEE MMM dd kk:mm:ss z yyyy
format.
+----------------------------+------------------------------+-------------------+
| date pattern you tried | your strdate input | pattern | parse |
| | | matched? | error? |
+----------------------------+------------------------------+----------+--------+
| EEE MMM dd kk:mm:ss z yyyy | Fri Apr 04 06:55:24 GMT 2014 | yes | no |
+----------------------------+------------------------------+----------+--------+
| dd-MMM-yyyy | Fri Apr 04 06:55:24 GMT 2014 | no | yes |
+----------------------------+------------------------------+----------+--------+
If you are trying to re format the same date string for display purposes, then
- construct a date object first with its default format received
- apply new pattern to return a string form of the same date.
Example:
String strdate = "Fri Apr 04 06:55:24 GMT 2014";
String currentPattern = "EEE MMM dd kk:mm:ss z yyyy";
SimpleDateFormat sdf = new SimpleDateFormat( currentPattern, Locale.ENGLISH );
Date dt = sdf.parse( strdate ); // new date is constructed
System.out.println( dt );
String newPattern = "dd-MMM-yyyy"; // new pattern is defined
sdf.applyPattern( newPattern ); // the same is applied
String new_strdate = sdf.format( dt ); // on the date object
System.out.println( new_strdate ); // resulting new string form of date
Output would be as below:
Fri Apr 04 06:55:24 GMT 2014
04-Apr-2014