mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2026-05-11 14:22:08 +08:00
[重大更新] 应广大用户要求 将Date换成LocalDateTime
This commit is contained in:
@@ -10,6 +10,7 @@ import java.lang.management.ManagementFactory;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -122,6 +123,16 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
return parseDateToStr(FormatsType.YYYY_MM_DD_HH_MM_SS, date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定 LocalDateTime 格式化为 YYYY-MM-DD HH:MM:SS 格式的字符串
|
||||
*
|
||||
* @param dateTime 要格式化的 LocalDateTime 对象
|
||||
* @return 格式化后的日期时间字符串
|
||||
*/
|
||||
public static String formatDateTime(final LocalDateTime dateTime) {
|
||||
return dateTime.format(DateTimeFormatter.ofPattern(FormatsType.YYYY_MM_DD_HH_MM_SS.getTimeFormat()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定日期按照指定格式进行格式化
|
||||
*
|
||||
@@ -242,7 +253,37 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
if (sec > 0) {
|
||||
result.append(String.format("%d秒", sec));
|
||||
}
|
||||
return result.length() > 0 ? result.toString().trim() : "0秒";
|
||||
return !result.isEmpty() ? result.toString().trim() : "0秒";
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个 LocalDateTime 时间点的差值(天、小时、分钟、秒),当值为0时不显示该单位
|
||||
*
|
||||
* @param endDate 结束时间
|
||||
* @param nowDate 当前时间
|
||||
* @return 时间差字符串,格式为 "x天 x小时 x分钟 x秒",若为 0 则不显示
|
||||
*/
|
||||
public static String getTimeDifference(LocalDateTime endDate, LocalDateTime nowDate) {
|
||||
Duration duration = Duration.between(nowDate, endDate);
|
||||
long day = duration.toDays();
|
||||
long hour = duration.toHours() % 24;
|
||||
long min = duration.toMinutes() % 60;
|
||||
long sec = duration.toSeconds() % 60;
|
||||
// 构建时间差字符串,条件是值不为0才显示
|
||||
StringBuilder result = new StringBuilder();
|
||||
if (day > 0) {
|
||||
result.append(String.format("%d天 ", day));
|
||||
}
|
||||
if (hour > 0) {
|
||||
result.append(String.format("%d小时 ", hour));
|
||||
}
|
||||
if (min > 0) {
|
||||
result.append(String.format("%d分钟 ", min));
|
||||
}
|
||||
if (sec > 0) {
|
||||
result.append(String.format("%d秒", sec));
|
||||
}
|
||||
return !result.isEmpty() ? result.toString().trim() : "0秒";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.dromara.common.core.utils;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import jakarta.servlet.ServletRequest;
|
||||
@@ -266,6 +267,21 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
return getClientIP(getRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端 IP 地址(支持自定义请求头)
|
||||
*
|
||||
* @param request 请求对象
|
||||
* @param otherHeaderNames 其他请求头名称
|
||||
* @return 客户端 IP 地址
|
||||
*/
|
||||
public static String getClientIP(HttpServletRequest request, String... otherHeaderNames) {
|
||||
String[] headers = {"X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
|
||||
if (ArrayUtil.isNotEmpty(otherHeaderNames)) {
|
||||
headers = ArrayUtil.addAll(headers, otherHeaderNames);
|
||||
}
|
||||
return JakartaServletUtil.getClientIP(request, headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对内容进行 URL 编码
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user