校验时间区划是否重叠或交叉

校验时间区划是否重叠或交叉


直接上代码

package com.creaze.study;

import java.util.ArrayList;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 欧阳锋feng
 * @version 1.0
 * @email tangziqian0001@163.com
 * @createDate 2021/10/9 16:30
 * @updateDate 2021/10/9 16:30
 **/
public class Test01 {

    public static class Aaa {
        private Long id;
        private Date beginTime;
        private Date endTime;
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public Date getBeginTime() { return beginTime; }
        public void setBeginTime(Date beginTime) { this.beginTime = beginTime; }
        public Date getEndTime() { return endTime; }
        public void setEndTime(Date endTime) { this.endTime = endTime; }
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Aaa a = new Aaa();
        a.setId(new Long(1));
        a.setBeginTime(sdf.parse("09:00:00"));
        a.setEndTime(sdf.parse("12:00:00"));
        Aaa b = new Aaa();
        b.setId(new Long(1));
        b.setBeginTime(sdf.parse("12:00:00"));
        b.setEndTime(sdf.parse("14:00:00"));
        Aaa c = new Aaa();
        c.setId(new Long(1));
        c.setBeginTime(sdf.parse("12:00:00"));
        c.setEndTime(sdf.parse("17:00:00"));
        List<Aaa> aaaList = new ArrayList<>();
        aaaList.add(a);
        aaaList.add(b);
        aaaList.add(c);
        System.out.println(time(aaaList));
    }

    public static String time(List<Aaa> aaaList){
        for (int i = 0; i<aaaList.size(); i++) {
            for (int k = i+1; k<aaaList.size(); k++) {
                System.out.print(aaaList.get(i).getBeginTime() + "  ");
                System.out.print(aaaList.get(i).getEndTime() + "  ");
                System.out.print(aaaList.get(k).getBeginTime() + "  ");
                System.out.println(aaaList.get(k).getEndTime());
                Boolean flag = IsInterSection(aaaList.get(i).getBeginTime(), aaaList.get(i).getEndTime(), aaaList.get(k).getBeginTime(), aaaList.get(k).getEndTime());
                if (!flag) return "第"+(i+1)+"行与第"+(k+1)+"行存在时间的重叠或交叉,请确认后重新提交!";
            }
        }
        return null;
    }

    public static Boolean IsInterSection(Date startDateOne,Date endDateOne,Date startDateTwo,Date endDateTwo) {

        // startDateOne 在 startDateTwo 与 endDateTwo 之间
        if (0 <= startDateOne.compareTo(startDateTwo) && startDateOne.compareTo(endDateTwo) < 0) {
            return false;
        }
        // endDateOne 在 startDateTwo 与 endDateTwo 之间
        if (0 < endDateOne.compareTo(startDateTwo) && endDateOne.compareTo(endDateTwo) <= 0) {
            return false;
        }

        // startDateTwo 在 startDateOne 与 endDateOne 之间
        if (0 <= startDateTwo.compareTo(startDateOne) && startDateTwo.compareTo(endDateOne) < 0) {
            return false;
        }
        // endDateTwo 在 startDateOne 与 endDateOne 之间
        if (0 < endDateTwo.compareTo(startDateOne) && endDateTwo.compareTo(endDateOne) <= 0) {
            return false;
        }

        return true;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧阳锋feng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值