0
点赞
收藏
分享

微信扫一扫

获取2个时间之间的所有年月,年月日,年月日时。

就是耍帅 2023-08-16 阅读 34



LocalDateTimeUtils.java


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LocalDateTimeUtils {
    private static final Logger logger = LoggerFactory.getLogger(LocalDateTimeUtils.class);
    //年月
    public static final String YEAR_MONTH = "yyyyMM";

    //年月日
    public static final String YEAR_MONTH_DAY = "yyyyMMdd";

    //年月日时
    public static final String YEAR_MONTH_HOURS = "yyyyMMddHH";

    public static List<String> getMonthsBetween(LocalDateTime start, LocalDateTime end, String timeFormat) {
        return Stream.iterate(start, d -> d.plusMonths(1))
                .limit(ChronoUnit.MONTHS.between(start, end) + 1)
                .map(localDateTime -> {
                    return localDateTime.format(DateTimeFormatter.ofPattern(timeFormat));
                })
                .collect(Collectors.toList());
    }

    public static List<String> getDaysBetween(LocalDateTime start, LocalDateTime end, String timeFormat) {
        return Stream.iterate(start, d -> d.plusDays(1))
                .limit(ChronoUnit.DAYS.between(start, end) + 1)
                .map(localDateTime -> {
                    return localDateTime.format(DateTimeFormatter.ofPattern(timeFormat));
                })
                .collect(Collectors.toList());
    }

    public static List<String> getHoursBetween(LocalDateTime start, LocalDateTime end, String timeFormat) {
        return Stream.iterate(start, d -> d.plusHours(1))
                .limit(ChronoUnit.HOURS.between(start, end) + 1)
                .map(localDateTime -> {
                    return localDateTime.format(DateTimeFormatter.ofPattern(timeFormat));
                })
                .collect(Collectors.toList());
    }


    public static void main(String[] args) {
        LocalDateTime b = LocalDateTime.of(2023,5,1, 13, 48,51);
        LocalDateTime e = LocalDateTime.of(2023,8,15, 16, 28,11);
        List<String> list1 = getMonthsBetween(b, e, YEAR_MONTH);
        logger.info(list1.toString());
        List<String> list2 = getDaysBetween(b, e, YEAR_MONTH_DAY);
        logger.info(list2.toString());
        List<String> list3 = getMonthsBetween(b, e, YEAR_MONTH_HOURS);
        logger.info(list3.toString());

        logger.info("---------l2--------------");
        List<LocalDateTime> l2 = Stream.iterate(b, p -> p.plusHours(1)).limit( ChronoUnit.HOURS.between(b, e) + 1 ).collect(Collectors.toList());
        for (LocalDateTime t: l2) {
            logger.info(t.toString());
        }

        logger.info("---------l0--------------");
        List<LocalDateTime> l0 = Stream.iterate(b, p -> p.plusDays(1)).limit( ChronoUnit.DAYS.between(b, e) + 1 ).collect(Collectors.toList());
        for (LocalDateTime t: l0) {
            logger.info(t.toString());
        }

        logger.info("---------l1--------------");
        List<LocalDateTime> l1 = Stream.iterate(b, p -> p.plusMonths(1)).limit( ChronoUnit.MONTHS.between(b, e) + 1 ).collect(Collectors.toList());
        for (LocalDateTime t: l1) {
            logger.info(t.toString());
        }



    }

}

输出:

获取2个时间之间的所有年月,年月日,年月日时。_java

主要使用的是先用ChronoUnit工具,

ChronoUnit.MONTHS.between(start,end)

先算出start到end有多少个指定单位的数量,简单就是说有多少个月,有多少天,有多少小时等;然后利用Stream.iterate来生成对应的集合迭代器

Stream.iterate(initial value, next value)  Stream.iterate(0, n -> n + 1).limit(10);
//这样会生成0,1,2,....9;

java9对Stream.iterate进行了增强。新增了第二个参数为退出条件。

Stream.iterate(1, n -> n < 20 , n -> n * 2)   .forEach(x -> System.out.println(x));
//1,2,4,8,16

然后Stream.iterate的start时间,然后进行每月,每天,每小时为单位进新新增得到集合。

上面的工具类再次使用了map对每个成员进行了格式化,然后形成新的列表。

举报

相关推荐

0 条评论