Java校验周期性是否存在重叠情况
- 工作小总结&小工具类
- 时间:2023-08-02 07:58
- 3003人已阅读
简介
场景描述:代码:/** * 校验周期时间是否叠加 * @param list * @return */ &
🔔🔔🔔好消息!好消息!🔔🔔🔔
有需要的朋友👉:联系凯哥
场景描述:
代码:
/** * 校验周期时间是否叠加 * @param list * @return */ public static ErrorTip checkCycleTime(List<CycleTimeVo> list) { if (CollectionUtils.size(list) < 2) { return null; } for (int i = 0; i < list.size(); i++) { CycleTimeVo timeVo = list.get(i); LocalTime startTime = LocalTime.parse(timeVo.getStartTimePoint()); LocalTime endTime = LocalTime.parse(timeVo.getEndTimePoint()); for (int j = 0; j < list.size(); j++) { if (i == j) { continue; } CycleTimeVo nextTimeVo = list.get(j); if (Objects.equals(timeVo.getStartWeek(), nextTimeVo.getStartWeek()) && Objects.equals(timeVo.getEndWeek(), nextTimeVo.getEndWeek()) && StringUtils.equals(timeVo.getStartTimePoint(), nextTimeVo.getStartTimePoint()) && StringUtils.equals(timeVo.getEndTimePoint(), nextTimeVo.getEndTimePoint())) { return new ErrorTip(500, String.format("周期时间有重叠[%s-%s]", i, j)); } LocalTime nextStartTime = LocalTime.parse(nextTimeVo.getStartTimePoint()); LocalTime nextEndTime = LocalTime.parse(nextTimeVo.getEndTimePoint()); boolean isInSide = nextTimeVo.getStartWeek() < nextTimeVo.getEndWeek() || Objects.equals(nextTimeVo.getStartWeek(), nextTimeVo.getEndWeek()) && nextStartTime.isBefore(nextEndTime); int nextStartWeek = nextTimeVo.getStartWeek(); int nextEndWeek = nextTimeVo.getEndWeek(); if (!isInSide) { nextStartWeek = nextTimeVo.getEndWeek(); nextEndWeek = nextTimeVo.getStartWeek(); LocalTime temp = nextStartTime; nextStartTime = nextEndTime; nextEndTime = temp; } boolean isAfterStart = nextStartWeek < timeVo.getStartWeek() || nextStartWeek == timeVo.getStartWeek() && (isInSide ? startTime.isAfter(nextStartTime) : !startTime.isBefore(nextStartTime)); boolean isBeforeEnd = timeVo.getStartWeek() < nextEndWeek || timeVo.getStartWeek() == nextEndWeek && (isInSide ? startTime.isBefore(nextEndTime) : !startTime.isAfter(nextEndTime)); if (isInSide == (isAfterStart && isBeforeEnd)) { return new ErrorTip(500, String.format("周期时间有重叠[%s-%s]", i, j)); } isAfterStart = nextStartWeek < timeVo.getEndWeek() || nextStartWeek == timeVo.getEndWeek() && (isInSide ? endTime.isAfter(nextStartTime) : !endTime.isBefore(nextStartTime)); isBeforeEnd = timeVo.getEndWeek() < nextEndWeek || timeVo.getEndWeek() == nextEndWeek && (isInSide ? endTime.isBefore(nextEndTime) : !endTime.isAfter(nextEndTime)); if (isInSide == (isAfterStart && isBeforeEnd)) { return new ErrorTip(500, String.format("周期时间有重叠[%s-%s]", i, j)); } } } return null; }
CycleTimeVo对象:
@Data public class CycleTimeVo implements Serializable { /** * 开始 周 */ private Integer startWeek; /** * 开始 时间点 * 12:01 */ private String startTimePoint; /** * 结束 周 */ private Integer endWeek; /** * 结束 时间点 * 18:45 */ private String endTimePoint; }
测试代码:
public static void main(String[] args) { List<CycleTimeVo> list = new ArrayList<>(); CycleTimeVo timeVo1 = new CycleTimeVo(); timeVo1.setStartWeek(7); timeVo1.setStartTimePoint("12:00"); timeVo1.setEndWeek(2); timeVo1.setEndTimePoint("12:00"); CycleTimeVo timeVo2 = new CycleTimeVo(); timeVo2.setStartWeek(2); timeVo2.setStartTimePoint("12:00"); timeVo2.setEndWeek(3); timeVo2.setEndTimePoint("12:00"); CycleTimeVo timeVo3 = new CycleTimeVo(); timeVo3.setStartWeek(2); timeVo3.setStartTimePoint("12:00"); timeVo3.setEndWeek(3); timeVo3.setEndTimePoint("12:00"); list.add(timeVo1); list.add(timeVo2); list.add(timeVo3); ErrorTip errorTip = checkCycleTime(list); System.out.println(JsonUtil.obj2Json(errorTip)); }