With Collections tools to achieve sorted by date
1, the date of collection of the sort
2, comprising a set of fields like the date field is sorted by date
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);
}
Outputs the print result:
[
Thu 01-Aug 2019 08:00:00 CST,
Thu 01-Aug 2019 15:00:00 CST,
Thu-Aug 01 18:00:00 CST 2019
]
If the class (e.g. CheckInformation for a date property field for the private Date checkTime), can be rewritten as:
Collections.sort(list,Comparator.comparing(CheckInformation::getCheckTime));