Reputation: 41759
The date strings comes from XML feed to my app in format like this Mon, 10 Dec 2012 13:18:23 GMT
and I would like to format is as "13:18:23". I have this method
private String formatTime(String time) {
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());
String temp = null;
try {
temp = df.format(time);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}
but I keep getting error IllegalArgumentException
.
Anyone can see that is going on with this code?
Upvotes: 0
Views: 2201
Reputation: 77904
String tmp = "Mon, 10 Dec 2012 13:18:23 GMT";
String DATE_FORMAT = "EEE, dd MMM yyyy kk:mm:ss zzz";
String DATE_FORMAT_NOW = "kk:mm:ss";
SimpleDateFormat sdfSource = new SimpleDateFormat(DATE_FORMAT);
Date date = sdfSource.parse(tmp);
SimpleDateFormat sdfDestination = new SimpleDateFormat(DATE_FORMAT_NOW);
tmp = sdfDestination.format(date);
System.out.println("Converted date is : " + tmp);
Output:
Converted date is : 15:18:23
You have difference +2 hours because of GMT. Remove zzz
from DATE_FORMAT
and you get:
13:18:23
Upvotes: 1
Reputation: 7415
df.format(time);
You are passing a String to the format() method, whereas it requires Date object.
See the docs here
Upvotes: 1