我一直得到这个例外,我不知道会出现什么问题。 有人可以帮助我吗? 下面的代码是解析项目(日期/时间戳),然后在列表视图中显示它们。 我知道它们是字符串,但它们需要添加到列表视图中。
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);
}
在Listview中添加项目的代码:
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;
}
答案 0 :(得分:0)