package com.trs.digital.module.manager.utils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class MonthStartEndDateTime {
    public static void main(String[] args) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 存储每个月的开始和结束时间
        List<String> monthStartEndList = new ArrayList<>();
        // 遍历过去12个月
        for (int i = 0; i < 12; i++) {
            // 当前月份向前推
            YearMonth yearMonth = YearMonth.from(currentDateTime).minusMonths(i);
            // 获取月初（第一天的00:00）
            LocalDateTime startOfMonth = yearMonth.atDay(1).atStartOfDay();
            // 获取月末（最后一天的23:59）
            LocalDateTime endOfMonth = yearMonth.atEndOfMonth().atTime(LocalTime.MAX);

            // 添加到列表
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            String formattedStart = startOfMonth.format(formatter);
            String formattedEnd = endOfMonth.format(formatter);
            monthStartEndList.add("Start: " + formattedStart + ", End: " + formattedEnd);
        }

        // 输出结果
        monthStartEndList.forEach(System.out::println);

    }
}
