现有过车记录集合ListAnalysisJudgmentVehicleVO AnalysisJudgmentVehicleVO属性有hpzlhphmgcsj均为String类型。根据过车记录集合有统计条件如下开始时间例2023-04-01结束时间例2023-04-10时间段0600-1800为白天其余时间为晚上。昼伏夜出比例30 计算要求同一天同一辆车晚上有过车记录白天没有即次数+1同一天号牌种类
以下是Java代码实现:
public List<AnalysisJudgmentVehicleVO> filterNightOutVehicles(List<AnalysisJudgmentVehicleVO> vehicles, String startDate, String endDate, String daytimeStart, String daytimeEnd, double ratio) {
// 将日期字符串转换为日期类型
LocalDate start = LocalDate.parse(startDate);
LocalDate end = LocalDate.parse(endDate);
LocalTime daytimeStartT = LocalTime.parse(daytimeStart);
LocalTime daytimeEndT = LocalTime.parse(daytimeEnd);
// 用于记录已经处理过的车辆
Set<String> processedVehicles = new HashSet<>();
// 用于记录满足条件的车辆
List<AnalysisJudgmentVehicleVO> filteredVehicles = new ArrayList<>();
// 遍历每一天
for (LocalDate date = start; date.isBefore(end.plusDays(1)); date = date.plusDays(1)) {
// 用于记录白天有过车记录的车辆
Set<String> daytimeVehicles = new HashSet<>();
// 遍历过车记录
for (AnalysisJudgmentVehicleVO vehicle : vehicles) {
// 获取过车时间
LocalDateTime time = LocalDateTime.parse(vehicle.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate vehicleDate = time.toLocalDate();
LocalTime vehicleTime = time.toLocalTime();
// 如果过车时间不在当前日期范围内,跳过
if (vehicleDate.isBefore(date) || vehicleDate.isAfter(date)) {
continue;
}
// 判断是否在白天时间段内
boolean isDaytime = vehicleTime.isAfter(daytimeStartT) && vehicleTime.isBefore(daytimeEndT);
// 如果是白天时间,记录车辆
if (isDaytime) {
daytimeVehicles.add(vehicle.getHpzl() + vehicle.getHphm());
} else {
// 如果是晚上时间,判断是否满足条件
String key = vehicle.getHpzl() + vehicle.getHphm();
if (!processedVehicles.contains(key) && !daytimeVehicles.contains(key)) {
// 计算时间差比例
long days = ChronoUnit.DAYS.between(date, time.toLocalDate());
double diffRatio = (double) days / (double) (end.toEpochDay() - start.toEpochDay());
if (diffRatio > ratio) {
// 满足条件,加入集合
filteredVehicles.add(vehicle);
}
processedVehicles.add(key);
}
}
}
}
return filteredVehicles;
}
该方法接受一个车辆过车记录集合vehicles,统计条件包括开始时间startDate,结束时间endDate,白天时间段daytimeStart和daytimeEnd,以及昼伏夜出比例ratio。方法返回满足条件的车辆过车记录集合。
首先,将日期字符串转换为日期类型,并定义两个集合processedVehicles和filteredVehicles,分别用于记录已经处理过的车辆和满足条件的车辆。然后,遍历每一天,对于每一天,先定义一个集合daytimeVehicles用于记录白天有过车记录的车辆。接着,遍历过车记录,对于每一条记录,获取过车时间,并判断是否在当前日期范围内。如果不在,跳过;如果在,判断是否在白天时间段内。如果是白天时间,记录车辆;如果是晚上时间,判断是否满足条件。判断条件包括在同一天同一辆车晚上有过车记录白天没有,以及同一天号牌种类号牌号码相同的车辆只算一次。如果满足条件,计算时间差比例,并与昼伏夜出比例比较。如果比例大于昼伏夜出比例,将过车记录添加到满足条件的车辆集合中。
最后,返回满足条件的车辆过车记录集合
原文地址: https://www.cveoy.top/t/topic/eEQF 著作权归作者所有。请勿转载和采集!