新增预警页面
This commit is contained in:
@@ -7,60 +7,5 @@ import java.util.Date;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
// 日期格式化器(线程安全,复用提升性能)
|
||||
private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE; // yyyy-MM-dd
|
||||
private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); // yyyy-MM
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||
|
||||
|
||||
public static String calculateStartCycleCode(String cycleType) {
|
||||
// 默认使用当前日期计算,留参便于测试时指定日期
|
||||
return calculateStartCycleCode(cycleType, LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载方法:支持指定基准日期计算起始cycleCode(便于测试)
|
||||
*
|
||||
* @param cycleType 周期类型(D:日, M:月, Q:季度, Y:年)
|
||||
* @param baseDate 基准日期(以此日期为起点计算时间范围)
|
||||
* @return 起始cycleCode(符合对应格式),未识别类型返回null
|
||||
*/
|
||||
public static String calculateStartCycleCode(String cycleType, LocalDate baseDate) {
|
||||
if (baseDate == null) {
|
||||
throw new IllegalArgumentException("基准日期不能为null");
|
||||
}
|
||||
if (cycleType == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return switch (cycleType) {
|
||||
case "D" ->
|
||||
// 日:最近30天,格式 yyyy-MM-dd
|
||||
baseDate.minusDays(30).format(DAY_FORMATTER);
|
||||
case "M" ->
|
||||
// 月:最近1年,格式 yyyy-MM
|
||||
baseDate.minusYears(1).format(MONTH_FORMATTER);
|
||||
case "Q" ->
|
||||
// 季度:最近3年,格式 yyyy-Qx
|
||||
getQuarterCycleCode(baseDate.minusYears(3));
|
||||
case "Y" ->
|
||||
// 年:最近6年,格式 yyyy
|
||||
String.valueOf(baseDate.minusYears(6).getYear());
|
||||
default ->
|
||||
// 未识别的周期类型,返回null
|
||||
null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算指定日期对应的季度cycleCode(格式:yyyy-Qx)
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 季度cycleCode(如2023-Q3)
|
||||
*/
|
||||
private static String getQuarterCycleCode(LocalDate date) {
|
||||
int month = date.getMonthValue(); // 1-12月
|
||||
int quarter = (month - 1) / 3 + 1;
|
||||
return String.format("%d-Q%d", date.getYear(), quarter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,4 +111,56 @@ public class vDate {
|
||||
result.append(seconds).append("秒");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String calculateStartCycleCode(String cycleType) {
|
||||
// 默认使用当前日期计算,留参便于测试时指定日期
|
||||
return calculateStartCycleCode(cycleType, LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载方法:支持指定基准日期计算起始cycleCode(便于测试)
|
||||
*
|
||||
* @param cycleType 周期类型(D:日, M:月, Q:季度, Y:年)
|
||||
* @param baseDate 基准日期(以此日期为起点计算时间范围)
|
||||
* @return 起始cycleCode(符合对应格式),未识别类型返回null
|
||||
*/
|
||||
public static String calculateStartCycleCode(String cycleType, LocalDate baseDate) {
|
||||
if (baseDate == null) {
|
||||
throw new IllegalArgumentException("基准日期不能为null");
|
||||
}
|
||||
if (cycleType == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return switch (cycleType) {
|
||||
case "D" ->
|
||||
// 日:最近30天,格式 yyyy-MM-dd
|
||||
baseDate.minusDays(30).format(DAY_FORMATTER);
|
||||
case "M" ->
|
||||
// 月:最近1年,格式 yyyy-MM
|
||||
baseDate.minusYears(1).format(MONTH_FORMATTER);
|
||||
case "Q" ->
|
||||
// 季度:最近3年,格式 yyyy-Qx
|
||||
getQuarterCycleCode(baseDate.minusYears(3));
|
||||
case "Y" ->
|
||||
// 年:最近6年,格式 yyyy
|
||||
String.valueOf(baseDate.minusYears(6).getYear());
|
||||
default ->
|
||||
// 未识别的周期类型,返回null
|
||||
null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算指定日期对应的季度cycleCode(格式:yyyy-Qx)
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 季度cycleCode(如2023-Q3)
|
||||
*/
|
||||
private static String getQuarterCycleCode(LocalDate date) {
|
||||
int month = date.getMonthValue(); // 1-12月
|
||||
int quarter = (month - 1) / 3 + 1;
|
||||
return String.format("%d-Q%d", date.getYear(), quarter);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user