Collections.sort(list, new Comparator<Notice>() {
@Override
public int compare(Notice lhs, Notice rhs) {
Long time1=Long.parseLong(OtherUtil.getTime(lhs.getIssue_datetime(), "yyyy-M-d HH:mm"));
Long time2=Long.parseLong(OtherUtil.getTime(rhs.getIssue_datetime(), "yyyy-M-d HH:mm"));
if( time1<time2){
return 1;
}else if( time1>time2){
return -1;
}
return 0;
}
@Override
public int compare(Notice lhs, Notice rhs) {
Long time1=Long.parseLong(OtherUtil.getTime(lhs.getIssue_datetime(), "yyyy-M-d HH:mm"));
Long time2=Long.parseLong(OtherUtil.getTime(rhs.getIssue_datetime(), "yyyy-M-d HH:mm"));
if( time1<time2){
return 1;
}else if( time1>time2){
return -1;
}
return 0;
}
});
/**
* 将字符串转为时间戳
* @param user_time 例如:user_time = "2015-1-3 09:03";
* @param format 格式 例如: "yyyy-M-d HH:mm";
* @return
*/
public static String getTime(String user_time,String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d;
if (isNull(user_time)) {
return "";
}
try {
d = sdf.parse(user_time);
long l = d.getTime();
return String.valueOf(l);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public static boolean isNull(String context) {
if ("".equals(context) || context == null) {
return true;
} else {
return false;
}
}