init
This commit is contained in:
113
ruoyi-xq/src/main/java/com/ruoyi/xq/util/Date77Util.java
Normal file
113
ruoyi-xq/src/main/java/com/ruoyi/xq/util/Date77Util.java
Normal 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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user