ArrayList<Inbox> inboxList;(Inbox是范型)
Collections.sort(inboxList, new Comparator<Inbox>() {
@Override
public int compare(Inbox inbox1, Inbox inbox2) {
Date date1 = stringToDate(inbox1.getMessageDate());
Date date2 = stringToDate(inbox2.getMessageDate());
// 对日期字段进行升序,如果欲降序可采用after方法
if (date1.before(date2)) {//before是指时间从最新时间下降到之前
return 1;
}
return -1;
}
});
public Date stringToDate(String dateString) {
ParsePosition position = new ParsePosition(0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateValue = simpleDateFormat.parse((19 + dateString), position);
return dateValue;
}
Collections.sort(inboxList, new Comparator<Inbox>() {
@Override
public int compare(Inbox inbox1, Inbox inbox2) {
Date date1 = stringToDate(inbox1.getMessageDate());
Date date2 = stringToDate(inbox2.getMessageDate());
// 对日期字段进行升序,如果欲降序可采用after方法
if (date1.before(date2)) {//before是指时间从最新时间下降到之前
return 1;
}
return -1;
}
});
public Date stringToDate(String dateString) {
ParsePosition position = new ParsePosition(0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateValue = simpleDateFormat.parse((19 + dateString), position);
return dateValue;
}