package annotatilonTest;
import java.util.Date;
class MyBean implements Comparable<MyBean> {
private Date sortKey;
public MyBean(Date date) {
super();
this.sortKey = date;
}
public Date getSortKey() {
return sortKey;
}
public void setSortKey(Date sortKey) {
this.sortKey = sortKey;
}
@Override
public int compareTo(MyBean o) {
return this.sortKey.compareTo(o.getSortKey());
}
@Override
public String toString() {
return "MyBean [sortKey=" + sortKey + "]";
}
}
package annotatilonTest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class sasd {
public static void main(String[] args) throws ParseException {
List<MyBean> list = new ArrayList<MyBean>();
String string1 = "2016-10-24 21:59:06";
String string2 = "2018-09-24 21:59:06";
String string3 = "2017-12-24 21:59:06";
String string4 = "2016-11-24 21:59:06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
MyBean a = new MyBean(sdf.parse(string1));
MyBean b = new MyBean(sdf.parse(string2));
MyBean c = new MyBean(sdf.parse(string3));
MyBean d = new MyBean(sdf.parse(string4));
list.add(a);
list.add(b);
list.add(c);
list.add(d);
for (MyBean myBean : list) {
sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(myBean.getSortKey()));
}
Collections.sort(list);
Collections.reverse(list);
System.out.println("---------------");
for (MyBean myBean : list) {
sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(myBean.getSortKey()));
}
}
}
Collections.sort(list);这是对集合的排序(前提是实体类里面实现接口重写方法)
Collections.reverse(list);对集合进行反转,原来是升续,现在变降序了,OK
大功告成