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

dev
AprilWind 8 months ago
parent ea429e79a9
commit fcf71dee33

@ -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;
} }

@ -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) {

@ -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 DAYSHOURSMINUTESSECONDSMILLISECONDSMICROSECONDSNANOSECONDS
* @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);
};
} }
/** /**

@ -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;
}
}
} }

Loading…
Cancel
Save