0
我不斷收到此異常,我不知道什麼是錯的。 有人可以幫我嗎? 下面的代碼是解析項目(日期/時間戳),然後在列表視圖中顯示它們。 我知道他們是字符串,但他們需要在列表視圖中添加。無法解析的日期在偏移量0)
java.text.ParseException: Unparseable date: "9 minutes ago" (at offset 0)
public static String formatDate(final long dt) {
return formatDate(new Date(dt));
}
public static String formatDate(final Date date) {
final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT);
return df.format(date);
}
public static final Date parseDate(final String date) throws ParseException {
final String pattern = "EEE MMM dd, yyyy h:mm a";
return parseDate(date, pattern);
}
public static final Date parseDate(final String date, final String pattern)
throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
return format.parse(date);
}
代碼添加在列表視圖中的項目:你有
private String getDateString(final Date d) {
if (d == null)
return "";
int[] elements1, elements2;
elements1 = getDateElements(System.currentTimeMillis());
elements2 = getDateElements(d.getTime());
if (elements1[0] == elements2[0] && elements1[1] == elements2[1]) {
if (elements1[2] == elements2[2])
return "Today";
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.add(Calendar.DAY_OF_MONTH, -1);
elements1 = getDateElements(c.getTimeInMillis());
if (elements1[2] == elements2[2])
return "Yesterday";
}
return Util.formatDate(d);
}
private int[] getDateElements(final long d) {
final int[] elements = new int[3];
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(d);
elements[0] = c.get(Calendar.YEAR);
elements[1] = c.get(Calendar.MONTH);
elements[2] = c.get(Calendar.DAY_OF_MONTH);
return elements;
}
嘗試字符串「10分鐘前」不能被解析爲日期時間。你不能那樣做。 –