0
点赞
收藏
分享

微信扫一扫

Java判断多个时间段是否重叠

杰森wang 2022-04-15 阅读 97
java

查询时间数组,格式化时间

List<EvaluationPlan> list1 = this.list(wrapper);
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date startTime = speechModel.getStartTime();
        Date endTime = speechModel.getEndTime();

        String startX = format.format(startTime);
        String endX = format.format(endTime);
        if (isNormal(startX, endX, list1)) {
            return true;
        } else {
            return false;
        }

判断传过来时间是否在数组中

public static boolean isNormal(String startX, String endX, List<EvaluationPlan> originList) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        if (!isEmpty(startX) && !isEmpty(endX)) {
            startX = formatDate(startX);
            endX = formatDate(endX);
            if (originList != null && originList.size() > 0) {
                List<String> startList = new ArrayList<String>();
                List<String> endList = new ArrayList<String>();
                for (int i = 0; i < originList.size(); i++) {
                    startList.add(formatDate(format.format(originList.get(i).getStartTime())));
                    endList.add(formatDate(format.format(originList.get(i).getEndTime())));
                }
                String minStart = startList.get(0);
                String maxEnd = endList.get(endList.size() - 1);
                if (endX.compareTo(minStart) < 0) {
                    return true;
                }
                if (startX.compareTo(maxEnd) > 0) {
                    return true;
                }
                if (startX.compareTo(minStart) > 0) {
                    for (int i = 0; i < startList.size(); i++) {
                        if (startX.compareTo(endList.get(i)) > 0) {
                            if (startX.compareTo(startList.get(i + 1)) < 0 && endX.compareTo(startList.get(i + 1)) < 0) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    public static boolean isEmpty(String object) {
        if (object == null) {
            object = "";
        }
        return object.length() == 0;
    }

    public static String formatDate(String date) {
        if (date != null) {
            date = date.replace("/", "").replace(":", "").replace(" ", "");
        } else {
            date = "";
        }
        return date;
    }
举报

相关推荐

0 条评论