Java 过滤昼伏夜出车辆数据
以下是一个可能的Java实现,用于过滤昼伏夜出车辆数据:
public List<AnalysisJudgmentVehicleVO> filterNightDriving(List<AnalysisJudgmentVehicleVO> records, String startTime, String endTime, double ratio) {
// 将开始和结束时间转换为Date类型
Date startDate = null, endDate = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
startDate = sdf.parse(startTime);
endDate = sdf.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
return new ArrayList<>();
}
// 计算开始和结束时间相差的天数
long diffDays = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
// 构建新集合
List<AnalysisJudgmentVehicleVO> result = new ArrayList<>();
// 遍历过车记录
Map<String, Set<String>> vehicleMap = new HashMap<>(); // 记录每个车辆每天是否有白天过车记录
for (AnalysisJudgmentVehicleVO record : records) {
// 将过车时间转换为Date类型
Date gcsj = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gcsj = sdf.parse(record.getGcsj());
} catch (ParseException e) {
e.printStackTrace();
continue;
}
// 判断是否在统计时间范围内
if (gcsj.before(startDate) || gcsj.after(endDate)) {
continue;
}
// 判断是否在白天时间内
Calendar cal = Calendar.getInstance();
cal.setTime(gcsj);
int hour = cal.get(Calendar.HOUR_OF_DAY);
if (hour >= 6 && hour < 18) { // 白天
// 标记该车辆当天有过白天过车记录
String key = record.getHpzl() + record.getHphm() + sdf.format(gcsj).substring(0, 10);
if (!vehicleMap.containsKey(key)) {
vehicleMap.put(key, new HashSet<>());
}
vehicleMap.get(key).add('day');
} else { // 晚上
// 判断该车辆当天是否已有白天过车记录
String key = record.getHpzl() + record.getHphm() + sdf.format(gcsj).substring(0, 10);
if (!vehicleMap.containsKey(key)) {
vehicleMap.put(key, new HashSet<>());
}
if (!vehicleMap.get(key).contains('day')) { // 没有白天过车记录
// 将过车数据添加到新集合中
result.add(record);
}
}
}
// 计算昼伏夜出比例
int nightDrivingCount = result.size();
int totalDrivingCount = vehicleMap.size();
double nightDrivingRatio = (double) nightDrivingCount / (diffDays * totalDrivingCount);
if (nightDrivingRatio > ratio) {
return result;
} else {
return new ArrayList<>();
}
}
使用示例:
List<AnalysisJudgmentVehicleVO> records = ...; // 输入过车记录集合
String startTime = "2023-04-01"; // 开始时间
String endTime = "2023-04-10"; // 结束时间
double ratio = 0.3; // 昼伏夜出比例
List<AnalysisJudgmentVehicleVO> result = filterNightDriving(records, startTime, endTime, ratio); // 过滤昼伏夜出数据
该代码实现的核心逻辑是:
- 将过车时间转换为Date类型,并根据开始时间和结束时间判断是否在统计时间范围内;
- 根据过车时间判断是白天还是晚上,并将车辆信息和日期作为key存储在map中,记录该车辆在该日期是否有白天过车记录;
- 如果该车辆在该日期没有白天过车记录,则将其添加到结果集中;
- 最后根据结果集的大小和统计时间范围计算昼伏夜出比例,判断是否满足条件。
该算法适用于对车辆数据进行分析,可以有效地过滤出昼伏夜出车辆,为相关人员提供参考信息。
原文地址: https://www.cveoy.top/t/topic/nVYo 著作权归作者所有。请勿转载和采集!