public class TestComparator {
public static void main(String[] args) {
List<DateDTO> dateList = new ArrayList<>();
DateDTO dateDTO1 = new DateDTO();
dateDTO1.setDate(DateUtil.stringToDate("2018-01-27"));
DateDTO dateDTO2 = new DateDTO();
dateDTO2.setDate(DateUtil.stringToDate("2018-01-25"));
DateDTO dateDTO3 = new DateDTO();
dateDTO3.setDate(DateUtil.stringToDate("2018-01-26"));
dateList.add(dateDTO1);
dateList.add(dateDTO2);
dateList.add(dateDTO3);
//按照日期升序排序
Comparator<DateDTO> comparator = (DateDTO d1,DateDTO d2) -> d1.getDate().compareTo(d2.getDate());
Collections.sort(dateList, comparator);
dateList.forEach(d ->{
System.out.println(DateUtil.dateToString(d.getDate()));
});
}
}
输出结果:
2018-01-25
2018-01-26
2018-01-27