|
|
@@ -7,6 +7,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.*;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.time.temporal.TemporalAdjusters;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
@@ -418,6 +419,11 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
|
|
return format.format(date);
|
|
|
}
|
|
|
|
|
|
+ public static String formatDateYYYYMMdd(Date date, String pattern) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(parsePatterns[1]);
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
public static String formatDateYYYYMMddHHmm(Date date) {
|
|
|
SimpleDateFormat format = new SimpleDateFormat(parsePatterns[2]);
|
|
|
return format.format(date);
|
|
|
@@ -554,39 +560,21 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
|
|
return today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
|
|
|
}
|
|
|
|
|
|
+ public static long getDaysBetween(Date startDate, Date endDate) {
|
|
|
+ // 将Date转换为LocalDate(需指定时区)
|
|
|
+ LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- // 当前日期
|
|
|
- LocalDate today = LocalDate.now();
|
|
|
- System.out.println("今天: " + today);
|
|
|
-
|
|
|
- // 明天
|
|
|
- LocalDate tomorrow = today.plusDays(1);
|
|
|
- System.out.println("明天: " + tomorrow);
|
|
|
-
|
|
|
- // 本周第一天(假设周一为一周开始)
|
|
|
- LocalDate firstDayOfThisWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
|
|
|
- System.out.println("本周第一天: " + firstDayOfThisWeek);
|
|
|
-
|
|
|
- // 下周第一天
|
|
|
- LocalDate firstDayOfNextWeek = firstDayOfThisWeek.plusWeeks(1);
|
|
|
- System.out.println("下周第一天: " + firstDayOfNextWeek);
|
|
|
-
|
|
|
- // 本月第一天
|
|
|
- LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
|
|
|
- System.out.println("本月第一天: " + firstDayOfThisMonth);
|
|
|
-
|
|
|
- // 下月第一天
|
|
|
- LocalDate firstDayOfNextMonth = today.plusMonths(1).withDayOfMonth(1);
|
|
|
- System.out.println("下月第一天: " + firstDayOfNextMonth);
|
|
|
+ // 计算天数差(end - start)
|
|
|
+ return ChronoUnit.DAYS.between(start, end);
|
|
|
+ }
|
|
|
|
|
|
- // 今年第一天
|
|
|
- LocalDate firstDayOfThisYear = today.withDayOfYear(1);
|
|
|
- System.out.println("今年第一天: " + firstDayOfThisYear);
|
|
|
|
|
|
- // 明年第一天
|
|
|
- LocalDate firstDayOfNextYear = today.plusYears(1).withDayOfYear(1);
|
|
|
- System.out.println("明年第一天: " + firstDayOfNextYear);
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Date date = StringToDate("2023-08-08 09:00:00", AFTConstants.YYYYMMDDHHMMSS);
|
|
|
+ Date date1 = StringToDate("2023-08-09 06:00:00", AFTConstants.YYYYMMDDHHMMSS);
|
|
|
+ long daysBetween = getDaysBetween(date, date1);
|
|
|
+ System.out.println(daysBetween);
|
|
|
}
|
|
|
|
|
|
}
|