package com.njcn.csdevice.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 类的介绍：
 * 二维码工具
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/8/21 13:57
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TimeUtil {

    public static List<String> getDateRangeAsString(String startDateStr, String endDateStr) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 解析为LocalDate对象
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.parse(endDateStr, formatter);
        // 计算天数差
        long numOfDays = ChronoUnit.DAYS.between(startDate, endDate) + 1;
        // 生成日期流，格式化为字符串并收集为列表
        return Stream.iterate(startDate, date -> date.plusDays(1))
                .limit(numOfDays)
                .map(formatter::format)
                .collect(Collectors.toList());
    }
}

