标签:throw 日期字段 form str get 打印 mat 集合排序 ati
借助Collections工具类,实现按照日期进行排序
1、日期集合排序
2、包含日期字段类的集合按照日期字段排序
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String a = "2019-08-01 08:00:00";
String b = "2019-08-01 18:00:00";
String c = "2019-08-01 15:00:00";
Date d1 = sdf.parse(a);
Date d2 = sdf.parse(b);
Date d3 = sdf.parse(c);
List<Date> list = new ArrayList<>();
list.add(d1);
list.add(d2);
list.add(d3);
Collections.sort(list, new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
});
System.out.println(list);
}
输出打印结果为:
[
Thu Aug 01 08:00:00 CST 2019,
Thu Aug 01 15:00:00 CST 2019,
Thu Aug 01 18:00:00 CST 2019
]
如果对于属性时日期的类(如 CheckInformation有一字段为 private Date checkTime),可以改写为:
Collections.sort(list,Comparator.comparing(CheckInformation::getCheckTime));
标签:throw 日期字段 form str get 打印 mat 集合排序 ati
原文地址:https://www.cnblogs.com/tank073/p/11330917.html