This commit is contained in:
张良(004796)
2024-01-31 22:36:50 +08:00
parent 98458c32fa
commit b725191b1a
11 changed files with 101 additions and 37 deletions

View File

@@ -3,9 +3,7 @@ package com.ruoyi.cai.util;
import cn.hutool.core.date.DateUtil;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
@@ -21,6 +19,56 @@ public class CaiDateUtil {
return ChronoUnit.SECONDS.between(after, before);
}
public static String betweenShowString(LocalDateTime minTime,LocalDateTime maxTime){
Duration duration = Duration.between(minTime, maxTime);
long seconds = duration.getSeconds();
long absSeconds = Math.abs(seconds);
long days = absSeconds / (60 * 60 * 24);
long hours = (absSeconds % (60 * 60 * 24)) / (60 * 60);
long minutes = (absSeconds % (60 * 60)) / 60;
if(days < 30){
return days+""+hours+"小时"+minutes+"分钟";
}
Period period = Period.between(minTime.toLocalDate(), maxTime.toLocalDate());
StringBuilder stringBuilder = new StringBuilder();
if(period.getYears() != 0){
stringBuilder.append(period.getYears()).append("");
}
if(period.getMonths() != 0){
stringBuilder.append(period.getMonths()).append("");
}
if(period.getDays() != 0){
stringBuilder.append(period.getDays()).append("");
}
return stringBuilder.toString();
}
public static void main(String[] args) {
// 两个时间点
LocalDateTime dateTime1 = LocalDateTime.of(2020, 5, 20, 12, 0, 0);
LocalDateTime dateTime2 = LocalDateTime.of(2024, 1, 15, 11, 30, 0);
// 计算日期间隔
Period period = Period.between(dateTime1.toLocalDate(), dateTime2.toLocalDate());
// 输出年、月、日间隔
System.out.println("间隔: " + period.getYears() + "" + period.getMonths() + " 个月 " + period.getDays() + "");
// 计算时间间隔
Duration duration = Duration.between(dateTime1, dateTime2);
// 输出时、分、秒间隔
long seconds = duration.getSeconds();
long absSeconds = Math.abs(seconds);
long days = absSeconds / (60 * 60 * 24);
long hours = (absSeconds % (60 * 60 * 24)) / (60 * 60);
long minutes = (absSeconds % (60 * 60)) / 60;
long secs = absSeconds % 60;
System.out.println("时间间隔: " + days + "" + hours + " 小时 " + minutes + " 分钟 " + secs + "");
}
public static LocalDate getLastWeekOne(LocalDate date){
@@ -42,6 +90,5 @@ public class CaiDateUtil {
public static String getCurrentTimeStr() {
return localDateTimeToString(LocalDateTime.now());
}
}