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);
}
}