This commit is contained in:
77
2024-03-21 23:04:40 +08:00
parent 4dd60a74f5
commit d0b1f961e8
41 changed files with 1048 additions and 62 deletions

View File

@@ -0,0 +1,113 @@
package com.ruoyi.xq.util;
import cn.hutool.core.date.DateUtil;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
public class Date77Util {
/**
* 计算时间相差秒数
* 前者-后者 的差值
*/
public static long diff(LocalDateTime before, LocalDateTime after){
return ChronoUnit.SECONDS.between(after, before);
}
public static long diffDays(LocalDateTime before, LocalDateTime after){
return ChronoUnit.DAYS.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;
long secs = absSeconds % 60;
if(days < 30){
StringBuilder sb = new StringBuilder();
if(days != 0){
sb.append(days).append("").append(hours).append("小时").append(minutes).append("分钟");
}else if(hours != 0){
sb.append(hours).append("小时").append(minutes).append("分钟");
}else if(minutes != 0){
sb.append(minutes).append("分钟");
}else {
sb.append(secs).append("");
}
return sb.toString();
}
Period period = Period.between(minTime.toLocalDate(), maxTime.toLocalDate());
StringBuilder sb = new StringBuilder();
if(period.getYears() != 0){
sb.append(period.getYears()).append("");
}
if(period.getMonths() != 0){
sb.append(period.getMonths()).append("");
}
if(period.getDays() != 0){
sb.append(period.getDays()).append("");
}
return sb.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){
return date.plusWeeks(-1)
.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
}
public static String localDateTimeToString(LocalDateTime localDateTime){
return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
public static LocalDateTime toDataSecond(Long date){
if(date == null){
return null;
}
return DateUtil.date(date*1000).toLocalDateTime();
}
public static String getCurrentTimeStr() {
return localDateTimeToString(LocalDateTime.now());
}
public static String formatDate(LocalDate begin) {
return begin.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
}