判断时间段是否有交集

问题:
小明的合同为2020-01-01至2020-01-31,工作时间为每天的09:00至18:00。有一个兼职项目,项目周期为2020-01-01至2020-01-31,工作时间为19:00至21:00,则小明可以报名参与些兼职。如果工作时间为17:00至21:00,则因为和本职工作时间有冲突不能报名。要求写一个方法判断时间段是否有交集。需要解决跨天问题。
解决方案:

package com.yungeng.volunteer.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import java.time.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Wesley
 * @create 2020/3/23
 */
public class Util {
    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TimeBucket {
        private LocalDateTime startTime;
        private LocalDateTime endTime;

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            TimeBucket that = (TimeBucket) o;
//            return !(this.startTime.after(that.endTime) || this.endTime.before(that.startTime));
//            return !(this.startTime.getTime()>=that.endTime.getTime() || this.endTime.getTime()<=that.startTime.getTime());
            return !(this.startTime.toEpochSecond(ZoneOffset.ofHours(8)) >= that.endTime.toEpochSecond(ZoneOffset.ofHours(8))
                    || this.endTime.toEpochSecond(ZoneOffset.ofHours(8)) <= that.startTime.toEpochSecond(ZoneOffset.ofHours(8)));
        }

        @Override
        public int hashCode() {
            return Objects.hash(startTime, endTime);
        }

        @Override
        public String toString() {
            return "TimeBucket{" +
                    "startTime=" + startTime +
                    ", endTime=" + endTime +
                    '}';
        }
    }

    public static List<TimeBucket> getTimeBucketList(LocalDate startDate, LocalDate endDate, LocalTime startTime, LocalTime endTime) {
        int days = differentDays(startDate, endDate);
        List<TimeBucket> timeBucketList = new ArrayList<>();
        //不跨天
        if (startTime.isBefore(endTime)) {
            for (int i = 0; i < days; i++) {
                timeBucketList.add(new TimeBucket(LocalDateTime.of(startDate, startTime).plusDays(i), LocalDateTime.of(startDate, endTime).plusDays(i)));
            }
            //跨天
        } else {
            for (int i = 0; i < days - 1; i++) {
                timeBucketList.add(new TimeBucket(LocalDateTime.of(startDate, startTime).plusDays(i), LocalDateTime.of(startDate, endTime).plusDays(1).plusDays(i)));
            }
        }
        return timeBucketList;
    }

    /**
     * 求两个时间点相隔天数,startDate = 2010-01-01, endDate = 2010-01-01 返回1天
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int differentDays(LocalDate startDate, LocalDate endDate) {
        return (int) (endDate.toEpochDay() - startDate.toEpochDay()) + 1;
    }

    public static LocalDate convertToLocalDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    public static void main(String[] args) throws Exception {

        List<TimeBucket> timeBucketList = new ArrayList<>();
        List<TimeBucket> myTimeBucketList = new ArrayList<>();

        LocalDate startDate = LocalDate.of(2020, 3, 23);
        LocalDate endDate = LocalDate.of(2020, 3, 23);
        System.out.println(differentDays(startDate, endDate));

        timeBucketList = getTimeBucketList(LocalDate.of(2020, 3, 1), LocalDate.of(2020, 4, 30), LocalTime.of(23, 0), LocalTime.of(2, 0));
        for (TimeBucket item : timeBucketList) {
            System.out.println(item);
        }
        myTimeBucketList = getTimeBucketList(LocalDate.of(2020, 3, 1), LocalDate.of(2020, 3, 31), LocalTime.of(1, 0), LocalTime.of(3, 0));
        ***System.out.println(CollectionUtils.containsAny(timeBucketList,myTimeBucketList));***
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值