您需要两次转换。例如:
private String formatDate(String date) {
SimpleDateFormat formatFrom = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z ");
java.util.Date tmpDate = formatFrom.parse(date);
SimpleDateFormat formatTo = new SimpleDateFormat("dd/MMM/yyyy HH:mm");
return formatTo.format(tmpDate);
}
对于我自己的 RSS 解析器,我使用以下代码来解析不同的日期格式:
if (value.contains("+")) {
value = value.substring(0, value.lastIndexOf("+") - 1);
}
String[] patterns = {//"EEE, dd MMM yyyy hh:mm:ss UTC",
"yyyy.MM.dd G 'at' HH:mm:ss z",
"EEE, MMM d, ''yy",
"yyyyy.MMMMM.dd GGG hh:mm aaa",
"EEE, d MMM yyyy HH:mm:ss Z",
"yyMMddHHmmssZ",
"d MMM yyyy HH:mm:ss z",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ssz",
"yyyy-MM-dd'T'HH:mm:ss.SSSz",
"EEE, d MMM yy HH:mm:ssz",
"EEE, d MMM yy HH:mm:ss",
"EEE, d MMM yy HH:mm z",
"EEE, d MMM yy HH:mm Z",
"EEE, d MMM yyyy HH:mm:ss z",
"EEE, d MMM yyyy HH:mm:ss Z",
"EEE, d MMM yyyy HH:mm:ss ZZZZ",
"EEE, d MMM yyyy HH:mm z",
"EEE, d MMM yyyy HH:mm Z",
"d MMM yy HH:mm z",
"d MMM yy HH:mm:ss z",
"d MMM yyyy HH:mm z",
"d MMM yyyy HH:mm:ss z"};
for (int i = 0; i < patterns.length; i++) {
SimpleDateFormat sdf = new SimpleDateFormat(patterns[i], Locale.ENGLISH);
try {
pubdate = sdf.parse(value);
break;
} catch (Exception e) {
}
}