I am trying to parse the date in a particular custom format.
WEDNESDAY 25th JAN 2012 - 12:44:07 PM
like this..
I created a SimpleDateFormat
for this..
SimpleDateFormat sdf = new SimpleDateFormat("EEEE DD MMM YYYY - HH:MM:SS aa" );
the problem is the literal for the days. it is coming like 25th, 23rd, 02nd.I am getting exception for this thing...
help how to overcome this problem.
You could split the date string you're trying to parse into parts and remove the offending two letters in the following way:
String text = "WEDNESDAY 21st JAN 2012 - 12:44:07 PM";
String[] parts = text.split(" ", 3); // we only need 3 parts. No need splitting more
parts[1] = parts[1].substring(0, 2);
String parseableText = String.format("%s %s %s", parts[0], parts[1], parts[2]);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd MMM yyyy - hh:mm:ss aa" );
try {
java.util.Date dt = sdf.parse(parseableText);
} catch (ParseException e) {
e.printStackTrace();
}
Your parse string had some errors in it as well. Case is important for the date and time ptterns. See the SimpleDateFormat javadoc for a reference.