|
@@ -3,12 +3,9 @@ package com.slibra.common.utils;
|
|
import java.lang.management.ManagementFactory;
|
|
import java.lang.management.ManagementFactory;
|
|
import java.text.ParseException;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
-import java.time.LocalDate;
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
-import java.time.LocalTime;
|
|
|
|
-import java.time.ZoneId;
|
|
|
|
-import java.time.ZonedDateTime;
|
|
|
|
|
|
+import java.time.*;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.time.format.DateTimeParseException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
@@ -252,6 +249,36 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ public static String getNYearSameDay(String inputDate, int n) {
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
|
|
|
|
+ LocalDate date = null;
|
|
|
|
+ try {
|
|
|
|
+ date = LocalDate.parse(inputDate, formatter);
|
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
|
+ System.out.println("输入的日期格式不正确,请使用yyyy-MM-dd格式。");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ LocalDate lastYearDate = date.minusYears(n);
|
|
|
|
+ return lastYearDate.format(formatter);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String getNYearSameMonth(String inputDate, int n) {
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
|
|
|
|
+ try {
|
|
|
|
+ // 解析输入的字符串为YearMonth对象
|
|
|
|
+ YearMonth currentYearMonth = YearMonth.parse(inputDate, formatter);
|
|
|
|
+ // 获取去年的同一月份
|
|
|
|
+ YearMonth previousYearMonth = currentYearMonth.minusYears(n);
|
|
|
|
+ // 格式化为字符串
|
|
|
|
+ return previousYearMonth.format(formatter);
|
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
|
+ System.err.println("输入的日期格式不正确,请使用yyyy-MM格式。");
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
Date nowDate = DateUtils.getNowDate();
|
|
Date nowDate = DateUtils.getNowDate();
|
|
Date date = plusDate(1, nowDate);
|
|
Date date = plusDate(1, nowDate);
|
|
@@ -259,5 +286,8 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
System.out.println(date);
|
|
System.out.println(date);
|
|
System.out.println(date1);
|
|
System.out.println(date1);
|
|
System.out.println(getMonthsBefore("2024-02", 2));
|
|
System.out.println(getMonthsBefore("2024-02", 2));
|
|
|
|
+ System.out.println();
|
|
|
|
+ System.out.println(getNYearSameDay("2024-02-01", 1));
|
|
|
|
+ System.out.println(getNYearSameMonth("2024-02", 1));
|
|
}
|
|
}
|
|
}
|
|
}
|