123
This commit is contained in:
@@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user