Java获取两个时间段的交并集

获取两个时间段的交并集

package com.yxs.project;

import java.time.LocalDate;
import java.util.*;

/**
 * @author lenovo
 */
public class DateAlgorithm {

    public final static int INTERSECTION = 0;
    public final static int UNION = 1;

    public static void main(String[] args) {
        List<LocalDate[]> localDates = twoTimePeriods(
                new LocalDate[]{LocalDate.of(2023, 8, 12), LocalDate.of(2023, 8, 15)},
                new LocalDate[]{LocalDate.of(2023, 8, 17), LocalDate.of(2023, 8, 17)},
                DateAlgorithm.UNION
        );
        if (localDates != null && !localDates.isEmpty()) {
            for (LocalDate[] periods : localDates) {
                System.out.println(Arrays.toString(periods));
            }
        }
    }

    /**
     * 获取两个时间段的交并集
     *
     * @param periods1 第一个时间段
     * @param periods2 第二个时间段
     * @param type 0 交集 1 并集
     * @return 不存在为null
     */
    public static List<LocalDate[]> twoTimePeriods(LocalDate[] periods1, LocalDate[] periods2, int type) {
        //校验时间段1是否有效
        boolean valid1 = Objects.nonNull(periods1)
                && periods1.length > 1
                && periods1[0] != null
                && periods1[1] != null
                && (periods1[0].isEqual(periods1[1]) || periods1[0].isBefore(periods1[1]));
        //校验时间段2是否有效
        boolean valid2 = Objects.nonNull(periods2)
                && periods2.length > 1
                && periods2[0] != null
                && periods2[1] != null
                && (periods2[0].isEqual(periods2[1]) || periods2[0].isBefore(periods2[1]));
        if (valid1 && valid2) {
            if (INTERSECTION == type) {
                LocalDate[] intersection = twoTimePeriodsIntersection(periods1, periods2);
                if (Objects.nonNull(intersection)) {
                    return Collections.singletonList(intersection);
                }
            }
            if (UNION == type) {
                return twoTimePeriodsUnion(periods1, periods2);
            }
        }
        return null;
    }

    /**
     * 获取两个时间段的交集
     *
     * @param periods1 第一个时间段
     * @param periods2 第二个时间段
     * @return 不存在为null
     */
    public static LocalDate[] twoTimePeriodsIntersection(LocalDate[] periods1, LocalDate[] periods2) {
        //判断两个时间段是否存在交集
        if (!(periods1[0].isAfter(periods2[1]) || periods2[0].isAfter(periods1[1]))) {
            return new LocalDate[]{
                    periods1[0].isAfter(periods2[0]) ? periods1[0] : periods2[0],
                    periods1[1].isBefore(periods2[1]) ? periods1[1] : periods2[1]
            };
        }
        return null;
    }

    /**
     * 获取两个时间段的并集
     *
     * @param periods1 第一个时间段
     * @param periods2 第二个时间段
     * @return 存在交集 一个 LocalDate[] 不存在 两个 LocalDate[]
     */
    public static List<LocalDate[]> twoTimePeriodsUnion(LocalDate[] periods1, LocalDate[] periods2) {
        List<LocalDate[]> periodsList = new ArrayList<>(2);
        //判断两个时间段是否存在交集
        if (periods1[0].isAfter(periods2[1]) || periods2[0].isAfter(periods1[1])) {
            //不存在
            periodsList.add(periods1);
            periodsList.add(periods2);
        } else {
            //存在
            periodsList.add(
                    new LocalDate[]{
                            periods1[0].isBefore(periods2[0]) ? periods1[0] : periods2[0],
                            periods1[1].isAfter(periods2[1]) ? periods1[1] : periods2[1]
                    }
            );
        }
        return periodsList;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值