|
@@ -8,6 +8,10 @@ import java.util.*;
|
|
|
|
|
|
|
|
public class SuperDateUtils {
|
|
public class SuperDateUtils {
|
|
|
|
|
|
|
|
|
|
+ private static final DateTimeFormatter YYYYMM = DateTimeFormatter.ofPattern("yyyy-MM");
|
|
|
|
|
+ private static final DateTimeFormatter YYYYMMDD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// ------------------------------------- 获取指定时间段内的所有日期 ---------------------------------------------------
|
|
// ------------------------------------- 获取指定时间段内的所有日期 ---------------------------------------------------
|
|
|
|
|
|
|
@@ -17,14 +21,14 @@ public class SuperDateUtils {
|
|
|
* @param startStr 起始日期,格式为yyyy-MM-dd
|
|
* @param startStr 起始日期,格式为yyyy-MM-dd
|
|
|
* @param endStr 结束日期,格式为yyyy-MM-dd
|
|
* @param endStr 结束日期,格式为yyyy-MM-dd
|
|
|
* @return 包含所有日期的列表(含起止日期)
|
|
* @return 包含所有日期的列表(含起止日期)
|
|
|
|
|
+ * java帮我写一个帮助类 可以通过传入时间段 获取所有的日期 如输入2025-07-23,2025-07-25,就可以输出2025-07-23、2025-07-24、2025-07-25
|
|
|
*/
|
|
*/
|
|
|
public static List<String> getAllDatesBetween(String startStr, String endStr) {
|
|
public static List<String> getAllDatesBetween(String startStr, String endStr) {
|
|
|
// 定义日期格式
|
|
// 定义日期格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
|
|
|
|
|
// 解析输入字符串为LocalDate对象
|
|
// 解析输入字符串为LocalDate对象
|
|
|
- LocalDate startDate = LocalDate.parse(startStr, formatter);
|
|
|
|
|
- LocalDate endDate = LocalDate.parse(endStr, formatter);
|
|
|
|
|
|
|
+ LocalDate startDate = LocalDate.parse(startStr, YYYYMMDD);
|
|
|
|
|
+ LocalDate endDate = LocalDate.parse(endStr, YYYYMMDD);
|
|
|
|
|
|
|
|
// 确保startDate <= endDate(自动纠正顺序)
|
|
// 确保startDate <= endDate(自动纠正顺序)
|
|
|
if (startDate.isAfter(endDate)) {
|
|
if (startDate.isAfter(endDate)) {
|
|
@@ -37,7 +41,7 @@ public class SuperDateUtils {
|
|
|
List<String> result = new ArrayList<>();
|
|
List<String> result = new ArrayList<>();
|
|
|
LocalDate current = startDate;
|
|
LocalDate current = startDate;
|
|
|
while (!current.isAfter(endDate)) { // 包含边界值
|
|
while (!current.isAfter(endDate)) { // 包含边界值
|
|
|
- result.add(current.format(formatter));
|
|
|
|
|
|
|
+ result.add(current.format(YYYYMMDD));
|
|
|
current = current.plusDays(1);
|
|
current = current.plusDays(1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -85,6 +89,7 @@ public class SuperDateUtils {
|
|
|
* @param endDateStr 结束日期,格式为yyyy-MM-dd
|
|
* @param endDateStr 结束日期,格式为yyyy-MM-dd
|
|
|
* @param weekday 星期几(如"周三"、"星期三")
|
|
* @param weekday 星期几(如"周三"、"星期三")
|
|
|
* @return 匹配的日期列表
|
|
* @return 匹配的日期列表
|
|
|
|
|
+ * java帮我写一个帮助类 可以通过传入时间段 获取指定周几的日期 如输入2025-07-23,2025-07-25,周三,就可以输出2025-07-23
|
|
|
*/
|
|
*/
|
|
|
public static List<String> getDatesByWeekday(String startDateStr, String endDateStr, String weekday) {
|
|
public static List<String> getDatesByWeekday(String startDateStr, String endDateStr, String weekday) {
|
|
|
// 校验星期参数
|
|
// 校验星期参数
|
|
@@ -94,9 +99,8 @@ public class SuperDateUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 解析日期
|
|
// 解析日期
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
- LocalDate startDate = LocalDate.parse(startDateStr, formatter);
|
|
|
|
|
- LocalDate endDate = LocalDate.parse(endDateStr, formatter);
|
|
|
|
|
|
|
+ LocalDate startDate = LocalDate.parse(startDateStr, YYYYMMDD);
|
|
|
|
|
+ LocalDate endDate = LocalDate.parse(endDateStr, YYYYMMDD);
|
|
|
|
|
|
|
|
// 自动校正日期顺序
|
|
// 自动校正日期顺序
|
|
|
if (startDate.isAfter(endDate)) {
|
|
if (startDate.isAfter(endDate)) {
|
|
@@ -110,7 +114,7 @@ public class SuperDateUtils {
|
|
|
LocalDate current = startDate;
|
|
LocalDate current = startDate;
|
|
|
while (!current.isAfter(endDate)) {
|
|
while (!current.isAfter(endDate)) {
|
|
|
if (current.getDayOfWeek() == targetDay) {
|
|
if (current.getDayOfWeek() == targetDay) {
|
|
|
- result.add(current.format(formatter));
|
|
|
|
|
|
|
+ result.add(current.format(YYYYMMDD));
|
|
|
}
|
|
}
|
|
|
current = current.plusDays(1);
|
|
current = current.plusDays(1);
|
|
|
}
|
|
}
|
|
@@ -132,10 +136,10 @@ public class SuperDateUtils {
|
|
|
* @param endStr 结束日期字符串,格式为yyyy-MM-dd
|
|
* @param endStr 结束日期字符串,格式为yyyy-MM-dd
|
|
|
* @param targetDay 目标日期的“日”部分(如24)
|
|
* @param targetDay 目标日期的“日”部分(如24)
|
|
|
* @return 符合条件的日期列表
|
|
* @return 符合条件的日期列表
|
|
|
|
|
+ * java帮我写一个帮助类 可以通过传入时间段 获取指定多少号的日期 如输入2025-06-23,2025-07-25,24,就可以输出2025-06-24和2025-07-24
|
|
|
*/
|
|
*/
|
|
|
public static List<String> getDatesWithDayInRange(String startStr, String endStr, int targetDay) {
|
|
public static List<String> getDatesWithDayInRange(String startStr, String endStr, int targetDay) {
|
|
|
// 解析输入的日期字符串
|
|
// 解析输入的日期字符串
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
LocalDate startDate = LocalDate.parse(startStr);
|
|
LocalDate startDate = LocalDate.parse(startStr);
|
|
|
LocalDate endDate = LocalDate.parse(endStr);
|
|
LocalDate endDate = LocalDate.parse(endStr);
|
|
|
|
|
|
|
@@ -159,7 +163,7 @@ public class SuperDateUtils {
|
|
|
LocalDate targetDate = currentYearMonth.atDay(targetDay);
|
|
LocalDate targetDate = currentYearMonth.atDay(targetDay);
|
|
|
// 检查目标日期是否在范围内
|
|
// 检查目标日期是否在范围内
|
|
|
if (!targetDate.isBefore(startDate) && !targetDate.isAfter(endDate)) {
|
|
if (!targetDate.isBefore(startDate) && !targetDate.isAfter(endDate)) {
|
|
|
- result.add(targetDate.format(formatter));
|
|
|
|
|
|
|
+ result.add(targetDate.format(YYYYMMDD));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
// 进入下一个月
|
|
// 进入下一个月
|
|
@@ -183,6 +187,7 @@ public class SuperDateUtils {
|
|
|
* @param endStr 结束日期字符串,格式为yyyy-MM-dd
|
|
* @param endStr 结束日期字符串,格式为yyyy-MM-dd
|
|
|
* @param dayOfMonth 每月的第几天(如1表示每月1日)
|
|
* @param dayOfMonth 每月的第几天(如1表示每月1日)
|
|
|
* @return 符合条件的日期列表
|
|
* @return 符合条件的日期列表
|
|
|
|
|
+ * java帮我写一个帮助类 可以通过传入时间段 获取指定每月第多少天的日期 如输入2025-08-23,2025-10-25,1,输出2025-09-01和2025-10-01,如果超过每月天数 如输入第天31天,则输出2025-09-30和2025-10-31
|
|
|
*/
|
|
*/
|
|
|
public static List<LocalDate> getDatesByDayOfMonth(String startStr, String endStr, int dayOfMonth) {
|
|
public static List<LocalDate> getDatesByDayOfMonth(String startStr, String endStr, int dayOfMonth) {
|
|
|
// 解析输入日期
|
|
// 解析输入日期
|
|
@@ -235,18 +240,17 @@ public class SuperDateUtils {
|
|
|
// dates4.forEach(System.out::println); // 输出:2025-03-31
|
|
// dates4.forEach(System.out::println); // 输出:2025-03-31
|
|
|
}*/
|
|
}*/
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
|
|
|
|
|
-
|
|
|
|
|
|
|
+// ---------------------------------------- 根据输入的年月获取相邻的前后月份 -----------------------------------------
|
|
|
/**
|
|
/**
|
|
|
* 根据输入的年月获取相邻的前后月份。
|
|
* 根据输入的年月获取相邻的前后月份。
|
|
|
* @param yearMonthStr 输入的年月字符串,格式为"yyyy-MM"
|
|
* @param yearMonthStr 输入的年月字符串,格式为"yyyy-MM"
|
|
|
* @return 包含前一个月和后一个月的列表,格式为["yyyy-MM", "yyyy-MM"]
|
|
* @return 包含前一个月和后一个月的列表,格式为["yyyy-MM", "yyyy-MM"]
|
|
|
|
|
+ * java帮我写一个帮助类 可以通过传入月份 获取相邻月份数据 如输入2025-08,输出2025-07和2025-09
|
|
|
*/
|
|
*/
|
|
|
public static List<String> getAdjacentMonths(String yearMonthStr) {
|
|
public static List<String> getAdjacentMonths(String yearMonthStr) {
|
|
|
// 将输入字符串补全为"yyyy-MM-01"以解析为LocalDate
|
|
// 将输入字符串补全为"yyyy-MM-01"以解析为LocalDate
|
|
|
String fullDateStr = yearMonthStr + "-01";
|
|
String fullDateStr = yearMonthStr + "-01";
|
|
|
- LocalDate date = LocalDate.parse(fullDateStr, FORMATTER);
|
|
|
|
|
|
|
+ LocalDate date = LocalDate.parse(fullDateStr, YYYYMMDD);
|
|
|
|
|
|
|
|
// 计算前一个月和后一个月
|
|
// 计算前一个月和后一个月
|
|
|
LocalDate previousMonth = date.minusMonths(1);
|
|
LocalDate previousMonth = date.minusMonths(1);
|
|
@@ -254,19 +258,20 @@ public class SuperDateUtils {
|
|
|
|
|
|
|
|
// 格式化为指定格式的字符串
|
|
// 格式化为指定格式的字符串
|
|
|
return Arrays.asList(
|
|
return Arrays.asList(
|
|
|
- previousMonth.format(FORMATTER),
|
|
|
|
|
- nextMonth.format(FORMATTER)
|
|
|
|
|
|
|
+ previousMonth.format(YYYYMM),
|
|
|
|
|
+ yearMonthStr,
|
|
|
|
|
+ nextMonth.format(YYYYMM)
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 示例用法
|
|
// 示例用法
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
|
|
+ /*public static void main(String[] args) {
|
|
|
List<String> adjacentMonths = getAdjacentMonths("2025-08");
|
|
List<String> adjacentMonths = getAdjacentMonths("2025-08");
|
|
|
System.out.println(adjacentMonths); // 输出: [2025-07, 2025-09]
|
|
System.out.println(adjacentMonths); // 输出: [2025-07, 2025-09]
|
|
|
|
|
|
|
|
// 更多测试案例
|
|
// 更多测试案例
|
|
|
System.out.println(getAdjacentMonths("2025-01")); // [2024-12, 2025-02]
|
|
System.out.println(getAdjacentMonths("2025-01")); // [2024-12, 2025-02]
|
|
|
System.out.println(getAdjacentMonths("2025-12")); // [2025-11, 2026-01]
|
|
System.out.println(getAdjacentMonths("2025-12")); // [2025-11, 2026-01]
|
|
|
- }
|
|
|
|
|
|
|
+ }*/
|
|
|
|
|
|
|
|
}
|
|
}
|