用于获取指定类型时间的开始时间和结束时间

package com.zd.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 时间范围工具类
 * 用于获取指定类型时间的开始时间和结束时间
 *
 * @author lingma
 */
public class DateTimeRangeUtils {

    /**
     * 时间类型常量
     */
    public static final String TODAY = "today";
    public static final String YESTERDAY = "yesterday";
    public static final String THIS_WEEK = "this_week";
    public static final String THIS_MONTH = "this_month";
    public static final String THIS_YEAR = "this_year";

    /**
     * 获取指定时间类型的时间范围
     *
     * @param type 时间类型 (today, yesterday, this_week, this_month, this_year)
     * @return 包含开始时间和结束时间的Map,key为start和end
     */
    public static Map<String, LocalDateTime> getTimeRange(String type) {
        return getTimeRange(type, LocalDateTime.now());
    }

    /**
     * 获取指定时间点的指定时间类型的时间范围
     *
     * @param type 时间类型 (today, yesterday, this_week, this_month, this_year)
     * @param date 指定的时间点
     * @return 包含开始时间和结束时间的Map,key为start和end
     */
    public static Map<String, LocalDateTime> getTimeRange(String type, LocalDateTime date) {
        Map<String, LocalDateTime> result = new HashMap<>();
        LocalDateTime targetDate = (date != null) ? date : LocalDateTime.now();

        switch (type) {
            case TODAY:
                result.put("start", targetDate.with(LocalTime.MIN));
                result.put("end", targetDate.with(LocalTime.MAX));
                break;
            case YESTERDAY:
                LocalDateTime yesterday = targetDate.minusDays(1);
                result.put("start", yesterday.with(LocalTime.MIN));
                result.put("end", yesterday.with(LocalTime.MAX));
                break;
            case THIS_WEEK:
                LocalDateTime startOfWeek = targetDate.with(DayOfWeek.MONDAY).with(LocalTime.MIN);
                LocalDateTime endOfWeek = targetDate.with(DayOfWeek.SUNDAY).with(LocalTime.MAX);
                result.put("start", startOfWeek);
                result.put("end", endOfWeek);
                break;
            case THIS_MONTH:
                LocalDateTime startOfMonth = targetDate.withDayOfMonth(1).with(LocalTime.MIN);
                LocalDateTime endOfMonth = targetDate.withDayOfMonth(targetDate.toLocalDate().lengthOfMonth()).with(LocalTime.MAX);
                result.put("start", startOfMonth);
                result.put("end", endOfMonth);
                break;
            case THIS_YEAR:
                LocalDateTime startOfYear = targetDate.withDayOfYear(1).with(LocalTime.MIN);
                LocalDateTime endOfYear = targetDate.withDayOfYear(targetDate.toLocalDate().lengthOfYear()).with(LocalTime.MAX);
                result.put("start", startOfYear);
                result.put("end", endOfYear);
                break;
            default:
                throw new IllegalArgumentException("不支持的时间类型: " + type);
        }

        return result;
    }

    /**
     * 获取指定时间类型的时间范围字符串形式
     *
     * @param type   时间类型 (today, yesterday, this_week, this_month, this_year)
     * @param format 时间格式
     * @return 包含开始时间和结束时间字符串的Map,key为start和end
     */
    public static Map<String, String> getTimeRangeString(String type, String format) {
        return getTimeRangeString(type, format, null);
    }

    /**
     * 获取指定时间点的指定时间类型的时间范围字符串形式
     *
     * @param type   时间类型 (today, yesterday, this_week, this_month, this_year)
     * @param format 时间格式
     * @param date   指定的时间点
     * @return 包含开始时间和结束时间字符串的Map,key为start和end
     */
    public static Map<String, String> getTimeRangeString(String type, String format, LocalDateTime date) {
        Map<String, LocalDateTime> timeRange = getTimeRange(type, date);
        Map<String, String> result = new HashMap<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        result.put("start", timeRange.get("start").format(formatter));
        result.put("end", timeRange.get("end").format(formatter));

        return result;
    }

    /**
     * 获取指定时间类型的时间范围,默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @param type 时间类型 (today, yesterday, this_week, this_month, this_year)
     * @return 包含开始时间和结束时间字符串的Map,key为start和end
     */
    public static Map<String, String> getTimeRangeString(String type) {
        return getTimeRangeString(type, "yyyy-MM-dd HH:mm:ss", null);
    }

    /**
     * 获取指定时间点的指定时间类型的时间范围,默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @param type 时间类型 (today, yesterday, this_week, this_month, this_year)
     * @param date 指定的时间点
     * @return 包含开始时间和结束时间字符串的Map,key为start和end
     */
    public static Map<String, String> getTimeRangeString(String type, LocalDateTime date) {
        return getTimeRangeString(type, "yyyy-MM-dd HH:mm:ss", date);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值