java 两个时间之间的 所有时间切片 工具

本文介绍了一种在Java中生成指定日期区间内所有日期的方法,通过三个实用函数实现,能够适用于LocalDate、LocalDateTime和泛型时间类型。作者分享了自定义API的代码实现,以帮助解决项目中常见的日期处理需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

项目里面 有这样的需求, 给你两个时间 10月1号 和10月20号。得出 1号到20号之间的 所有天。例如 1号,2号,3号,4号,…20号。

java没有原生的api,或者 是我没找到。 自己写了一个,觉得有些 意思 ,就发篇博客吧

直接上代码


     /**
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param chronoUnit 时间刻度 note 当你传入的时间 是localTime 时候 ,时间刻度 就不能为Day
     * @return
     */
    private static <T> T[] intervalTimesType(Temporal startTime, Temporal endTime, ChronoUnit chronoUnit) {
        Objects.requireNonNull(startTime);
        Objects.requireNonNull(endTime);
        Objects.requireNonNull(chronoUnit);
        long interval = startTime.until(endTime, chronoUnit);
        //这里使用 linkList
        List<Temporal> result = new LinkedList<>();
        for (int i = 0; i <= interval; i++) {
            Temporal temp = startTime.plus(i, chronoUnit);
            result.add(temp);
        }
        return result.toArray((T[]) Array.newInstance(startTime.getClass(), 0));
    }

    /**
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param chronoUnit 时间刻度
     * @return
     */
    public static LocalDateTime[] intervalTimes(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit chronoUnit) {
        Objects.requireNonNull(startTime);
        Objects.requireNonNull(endTime);
        Objects.requireNonNull(chronoUnit);

        long interval = startTime.until(endTime, chronoUnit);
        //这里使用 linkList
        List<LocalDateTime> result = new LinkedList<>();
        for (int i = 0; i <= interval; i++) {
            LocalDateTime temp = startTime.plus(i, chronoUnit);
            result.add(temp);
        }
        return result.toArray(new LocalDateTime[]{});
    }

    /**
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param chronoUnit 时间刻度
     * @return
     */
    public static LocalDate[] intervalTimes(LocalDate startTime, LocalDate endTime, ChronoUnit chronoUnit) {
        Objects.requireNonNull(startTime);
        Objects.requireNonNull(endTime);
        Objects.requireNonNull(chronoUnit);

        long interval = startTime.until(endTime, chronoUnit);
        //这里使用 linkList
        List<LocalDate> result = new LinkedList<>();
        for (int i = 0; i <= interval; i++) {
            LocalDate temp = startTime.plus(i, chronoUnit);
            result.add(temp);
        }
        return result.toArray(new LocalDate[]{});
    }

使用

       LocalDateTime now = LocalDateTime.now();
        //提前四个小时
        LocalDateTime beforeTime = now.minusHours(4);
        System.out.println("泛型》》》》》》");
        LocalTime[] objects = intervalTimesType(beforeTime.toLocalTime(), now.toLocalTime(), ChronoUnit.HOURS);
        Arrays.asList(objects).forEach(System.out::println);

       beforeTime = now.minusDays(3);
        //localdate
        System.out.println("非泛型》》》》》》");
        LocalDateTime[] intervalTimes = intervalTimes(beforeTime, now, ChronoUnit.DAYS);
        Arrays.asList(intervalTimes).forEach(System.out::println);

结果

在这里插入图片描述
定义了 三个方法 ,第一个是泛型 比较推荐,后面两个都是指定类型的。随便用

如果有不懂的,可以关注我的公众号 “知我饭否” 向我留言。我会每天更新一些文章,有兴趣的可以 微信 搜索"知我饭否" or 扫描下方二维码

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值