update 优化日期与字符串工具类

This commit is contained in:
AprilWind
2025-04-25 13:46:07 +08:00
parent ea429e79a9
commit fcf71dee33
4 changed files with 58 additions and 13 deletions

View File

@@ -5,7 +5,6 @@ import lombok.Getter;
/** /**
* 设备类型 * 设备类型
* 针对一套 用户体系
* *
* @author Lion Li * @author Lion Li
*/ */
@@ -26,7 +25,15 @@ public enum DeviceType {
/** /**
* 小程序端 * 小程序端
*/ */
XCX("xcx"); XCX("xcx"),
/**
* 第三方社交登录平台
*/
SOCIAL("social");
/**
* 设备标识
*/
private final String device; private final String device;
} }

View File

@@ -1,12 +1,11 @@
package org.dromara.common.core.enums; package org.dromara.common.core.enums;
import org.dromara.common.core.utils.StringUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.dromara.common.core.utils.StringUtils;
/** /**
* 设备类型 * 用户类型
* 针对多套 用户体系
* *
* @author Lion Li * @author Lion Li
*/ */
@@ -15,15 +14,18 @@ import lombok.Getter;
public enum UserType { public enum UserType {
/** /**
* pc端 * 后台系统用户
*/ */
SYS_USER("sys_user"), SYS_USER("sys_user"),
/** /**
* app端 * 移动客户端用户
*/ */
APP_USER("app_user"); APP_USER("app_user");
/**
* 用户类型标识(用于 token、权限识别等
*/
private final String userType; private final String userType;
public static UserType getUserType(String str) { public static UserType getUserType(String str) {

View File

@@ -175,14 +175,27 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
} }
/** /**
* 计算两个日期之间的天数差(以毫秒为单位 * 计算两个时间之间的时间差,并以指定单位返回(绝对值
* *
* @param date1 第一个日期 * @param start 起始时间
* @param date2 第二个日期 * @param end 结束时间
* @return 两个日期之间的天数差的绝对值 * @param unit 所需返回的时间单位DAYS、HOURS、MINUTES、SECONDS、MILLISECONDS、MICROSECONDS、NANOSECONDS
* @return 时间差的绝对值,以指定单位表示
*/ */
public static int differentDaysByMillisecond(Date date1, Date date2) { public static long difference(Date start, Date end, TimeUnit unit) {
return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); // 计算时间差,单位为毫秒,取绝对值避免负数
long diffInMillis = Math.abs(end.getTime() - start.getTime());
// 根据目标单位转换时间差
return switch (unit) {
case DAYS -> diffInMillis / TimeUnit.DAYS.toMillis(1);
case HOURS -> diffInMillis / TimeUnit.HOURS.toMillis(1);
case MINUTES -> diffInMillis / TimeUnit.MINUTES.toMillis(1);
case SECONDS -> diffInMillis / TimeUnit.SECONDS.toMillis(1);
case MILLISECONDS -> diffInMillis;
case MICROSECONDS -> TimeUnit.MILLISECONDS.toMicros(diffInMillis);
case NANOSECONDS -> TimeUnit.MILLISECONDS.toNanos(diffInMillis);
};
} }
/** /**

View File

@@ -6,6 +6,7 @@ import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -339,4 +340,26 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return false; return false;
} }
/**
* 将字符串从源字符集转换为目标字符集
*
* @param input 原始字符串
* @param fromCharset 源字符集
* @param toCharset 目标字符集
* @return 转换后的字符串
*/
public static String convert(String input, Charset fromCharset, Charset toCharset) {
if (isBlank(input)) {
return input;
}
try {
// 从源字符集获取字节
byte[] bytes = input.getBytes(fromCharset);
// 使用目标字符集解码
return new String(bytes, toCharset);
} catch (Exception e) {
return input;
}
}
} }