Simpledateformat Parseexception
Solution 1:
It appears Android's z
does not accept time zones in the format XXX (such as "CET"). (Pulling from the SimpleDateFormat
documentation.)
Try this instead:
Stringtime="Fri, 02 Nov 2012 11:58 pm +0100"; // CET = +1hr = +0100SimpleDateFormatparseFormat=newSimpleDateFormat("EEE, dd MMM yyyy hh:mm aa Z"); // Capital ZDatedate= parseFormat.parse(time);
SimpleDateFormatdisplayFormat=newSimpleDateFormat("dd.MM.yyyy, HH:mm");
System.out.println("output is " + displayFormat.format(date));
output is 02.11.2012, 22:58
Note: Also, I think you meant hh
instead of HH
, since you have PM
.
Result is shown here. (This uses Java7's SimpleDateFormat
, but Android should support RFC 822 timezones (+0100
) as well.)
NB: Also, as it appears Android's z
accepts full names ("Pacific Standard Time" is the example they give), you could simply specify "Centural European Time" instead of "CET".
Solution 2:
Try out the following code:
SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMMdd");
System.out.println(date_format.format(cal.getTime()));
It will work.. If not print the log cat? What erroe is coming?
Solution 3:
First of All I must agree with @Eric answer.
You just need to remove "CET" from your string of date.
Here is sample code. Check it.
Stringtime="Fri, 02 Nov 2012 11:58 pm CET";
time = time.replaceAll("CET", "").trim();
SimpleDateFormatdisplayFormat=newSimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormatparseFormat=newSimpleDateFormat("EEE, dd MMM yyyy HH:mm aa");
Datedate=null;
try {
date = parseFormat.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("output is " + displayFormat.format(date));
Post a Comment for "Simpledateformat Parseexception"