如题 需求很简单 但是实现起来有些麻烦
// 按打卡时间排序
List<DailyRecords> recordListSorted = dailyRecords.stream().sorted((o1, o2) -> {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = sdf.parse(o1.getRecordInfo().substring(o1.getRecordInfo().indexOf("time") + 7, o1.getRecordInfo().indexOf("record_type") - 3));
Date date2 = sdf.parse(o2.getRecordInfo().substring(o2.getRecordInfo().indexOf("time") + 7, o2.getRecordInfo().indexOf("record_type") - 3));
return Long.compare(date1.getTime(), date2.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}).collect(Collectors.toList());

这段代码展示了如何使用Java 8的Stream API对DailyRecords列表进行排序,依据是记录中的时间信息。它首先解析记录中的日期字符串,然后比较两个日期的毫秒值来完成排序。如果遇到日期解析异常,会捕获并打印堆栈跟踪,同时返回0保持原有的顺序。
1591

被折叠的 条评论
为什么被折叠?



