日期生成工具类(0 - 当天, 1 - 当周, 2 - 当月, 3 - 当年, 4 - 当前周今天向前推7天, 5 - 从当前日期倒推一年, 6 - 从当前日期倒推一个月 7 - 本季度 )

方法1 0 - 当天, 1 - 当周, 2 - 当月, 3 - 当年, 4 - 当前周今天向前推7天, 5 - 从当前日期倒推一年, 6 - 从当前日期倒推一个月 7 - 本季度, 8 - 第一季度, 9 - 第二季度, 10 - 第三季度, 11 - 第四季度, 12 - 上半年, 13 - 下半年
方法2 0 - 小时列表(当天)1 - 当前年月列表(当月) 2 - 当前年列表(当年) 3 - 当前时间倒推24小时 4 - 当前时间倒推30天 5 - 当前时间倒推一年 6 - 当前时间倒推一年

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DateGenerationUtil {
    /**
     * 根据输入类型生成日期时间数组
     *
     * @param type 0 - 当天, 1 - 当周, 2 - 当月, 3 - 当年, 4 - 当前周今天向前推7天, 5 - 从当前日期倒推一年, 6 - 从当前日期倒推一个月
     * @return 日期时间数组 [开始日期时间, 结束日期时间]
     */
    public static String[] generateDateRange(String type) {
        LocalDateTime startDateTime;
        LocalDateTime endDateTime;
        LocalDate today = LocalDate.now();

        switch (type) {
            case "0":
                // 当天
                startDateTime = today.atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "1":
                // 当周
                startDateTime = today.with(DayOfWeek.MONDAY).atStartOfDay();
                endDateTime = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX);
                break;
            case "2":
                // 当月
                startDateTime = today.with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "3":
                // 当年
                startDateTime = today.with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
                endDateTime = today.with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
                break;
            case "4":
                // 当前周今天向前推7天
                startDateTime = today.minusDays(7).atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "5":
                // 从当前日期倒推一年
                startDateTime = today.minusYears(1).atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "6":
                // 从当前日期倒推一个月
                startDateTime = today.minusMonths(1).atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "7":
                // 本季度
                int currentMonth = today.getMonthValue();
                int quarterStartMonth = ((currentMonth - 1) / 3) * 3 + 1;
                int quarterEndMonth = quarterStartMonth + 2;

                LocalDate startOfQuarter = today.withMonth(quarterStartMonth).with(TemporalAdjusters.firstDayOfMonth());
                LocalDate endOfQuarter = today.withMonth(quarterEndMonth).with(TemporalAdjusters.lastDayOfMonth());

                startDateTime = startOfQuarter.atStartOfDay();
                endDateTime = endOfQuarter.atTime(LocalTime.MAX);
                break;
            case "8":
                // 第一季度
                startDateTime = today.withMonth(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(3).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "9":
                // 第二季度
                startDateTime = today.withMonth(4).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(6).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "10":
                // 第三季度
                startDateTime = today.withMonth(7).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(9).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "11":
                // 第四季度
                startDateTime = today.withMonth(10).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(12).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "12":
                // 上半年
                startDateTime = today.withMonth(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(6).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "13":
                // 下半年
                startDateTime = today.withMonth(7).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(12).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;


            default:
                throw new IllegalArgumentException("无效的类型: " + type);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        return new String[]{formatter.format(startDateTime), formatter.format(endDateTime)};
    }


    public static LocalDateTime[] generateDateRangeLocalDateTime(String type) {
        LocalDateTime startDateTime;
        LocalDateTime endDateTime;
        LocalDate today = LocalDate.now();

        switch (type) {
            case "0":
                // 当天
                startDateTime = today.atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "1":
                // 当周
                startDateTime = today.with(DayOfWeek.MONDAY).atStartOfDay();
                endDateTime = today.with(DayOfWeek.SUNDAY).atTime(LocalTime.MAX);
                break;
            case "2":
                // 当月
                startDateTime = today.with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "3":
                // 当年
                startDateTime = today.with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
                endDateTime = today.with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
                break;
            case "4":
                // 当前周今天向前推7天
                startDateTime = today.minusDays(7).atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "5":
                // 从当前日期倒推一年
                startDateTime = today.minusYears(1).atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "6":
                // 从当前日期倒推一个月
                startDateTime = today.minusMonths(1).atStartOfDay();
                endDateTime = today.atTime(LocalTime.MAX);
                break;
            case "7":
                // 本季度
                int currentMonth = today.getMonthValue();
                int quarterStartMonth = ((currentMonth - 1) / 3) * 3 + 1;
                int quarterEndMonth = quarterStartMonth + 2;

                LocalDate startOfQuarter = today.withMonth(quarterStartMonth).with(TemporalAdjusters.firstDayOfMonth());
                LocalDate endOfQuarter = today.withMonth(quarterEndMonth).with(TemporalAdjusters.lastDayOfMonth());

                startDateTime = startOfQuarter.atStartOfDay();
                endDateTime = endOfQuarter.atTime(LocalTime.MAX);
                break;
            case "8":
                // 第一季度
                startDateTime = today.withMonth(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(3).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "9":
                // 第二季度
                startDateTime = today.withMonth(4).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(6).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "10":
                // 第三季度
                startDateTime = today.withMonth(7).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(9).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "11":
                // 第四季度
                startDateTime = today.withMonth(10).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(12).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "12":
                // 上半年
                startDateTime = today.withMonth(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(6).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;
            case "13":
                // 下半年
                startDateTime = today.withMonth(7).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
                endDateTime = today.withMonth(12).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
                break;

            default:
                throw new IllegalArgumentException("无效的类型: " + type);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        return new LocalDateTime[]{startDateTime, endDateTime};
    }

    /**
     * 根据传入的类型返回相应的日期/时间列表
     * 0 - 小时列表(当天)1 - 当前年月列表(当月) 2 - 当前年列表(当年) 3 - 当前时间倒推24小时 4 - 当前时间倒推30天 5 - 当前时间倒推一年
     * @param type 日期/时间类型
     * @return 对应的日期/时间列表
     */
    public static List<String> generateDateList(String type) {
        List<String> resultList = new ArrayList<>();
        DateTimeFormatter formatter;

        switch (type) {
            case "0": // 小时列表
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
                LocalDate currentDate = LocalDate.now();
                LocalTime currentTime = LocalTime.now();
                int currentHour = currentTime.getHour();
                for (int i = 0; i <= currentHour; i++) {
                    String timeString = currentDate.atTime(LocalTime.of(i, 0)).format(formatter);
                    resultList.add(timeString);
                }
                break;

            case "1": // 当前年月列表
                formatter = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDate today = LocalDate.now();
                for (int month = 1; month <= 12; month++) {
                    LocalDate date = today.withMonth(month);
                    resultList.add(date.format(formatter));
                }
                break;

            case "2": // 当前月份的天列表
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate currentMonthDate = LocalDate.now();
                int daysInMonth = currentMonthDate.lengthOfMonth();
                for (int day = 1; day <= daysInMonth; day++) {
                    LocalDate date = LocalDate.of(currentMonthDate.getYear(), currentMonthDate.getMonth(), day);
                    resultList.add(date.format(formatter));
                }
                break;

            case "3": // 倒推24小时的日期时间列表
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
                LocalDateTime currentTime24 = LocalDateTime.now();
                for (int i = 0; i < 24; i++) {
                    LocalDateTime time = currentTime24.minusHours(i);
                    resultList.add(time.format(formatter));
                }
                break;

            case "4": // 倒推30天的日期列表
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate currentDate30 = LocalDate.now();
                for (int i = 0; i < 30; i++) {
                    LocalDate date = currentDate30.minusDays(i);
                    resultList.add(date.format(formatter));
                }
                break;

            case "5": // 倒推365天的日期列表,每月一次
                formatter = DateTimeFormatter.ofPattern("yyyy-MM");
                LocalDate currentDate365 = LocalDate.now();

                // 倒推365天,每个月的第一天
                for (int i = 0; i < 12; i++) {
                    LocalDate date = currentDate365.minusMonths(i).withDayOfMonth(1); // 获取每个月的第一天
                    resultList.add(date.format(formatter));
                }
                break;
            case "6": // 倒推4年的日期列表,每月一次
                formatter = DateTimeFormatter.ofPattern("yyyy");
                LocalDate currentYear = LocalDate.now();
                for (int i = 0; i < 5; i++) {
                    LocalDate date = currentYear.minusYears(i);
                    if (i != 0) {
                        resultList.add(date.format(formatter));
                    }

                }


                break;


            default:
                throw new RuntimeException("无效的类型");
        }

        return resultList;
    }

    /**
     * 传入月份返回第一天和最后一天
     * @param month
     * @return
     */
    public static LocalDateTime[] getFirstAndLastDayOfMonth(String month) {
        // 定义日期格式化器
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");

        // 使用YearMonth类来处理只包含年和月的字符串
        YearMonth yearMonth = YearMonth.parse(month, monthFormatter);

        // 获取当月的第一天 00:00:00
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        LocalDateTime startOfDay = firstDayOfMonth.atStartOfDay();

        // 获取当月的最后一天 23:59:59
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime endOfDay = lastDayOfMonth.atTime(23, 59, 59);

        return new LocalDateTime[]{startOfDay, endOfDay};
    }

    /**
     * 传入年份返回第一和最后一天
     * @param yearStr
     * @return
     */

    public static LocalDateTime[] getFirstAndLastDayOfYear(String yearStr) {
        // 定义日期格式化器,只包含年份
        DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy");

        // 使用Year类来处理只包含年的字符串
        Year year = Year.parse(yearStr, yearFormatter);

        // 获取当年的第一天 00:00:00
        LocalDate firstDayOfYear = year.atDay(1);
        LocalDateTime startOfDay = firstDayOfYear.atStartOfDay();

        // 获取当年的最后一天 23:59:59
        LocalDate lastDayOfYear = year.atDay(year.length()); // year.length() 获取该年有多少天(处理闰年)
        LocalDateTime endOfDay = lastDayOfYear.atTime(23, 59, 59);

        return new LocalDateTime[]{startOfDay, endOfDay};
    }


    public static void main(String[] args) {
        System.out.println(DateGenerationUtil.generateDateList("6"));

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值