我一直以为fastjson的坑是最多的,今天用了一下Gson,它的坑也不少,Long类型在反序列化的时候会将数据转化为科学计数法。
2024-11-06 start:
package com.gmcc.msb.util;
/**
* @author haijin
* @description: 解析文本工具类
* @date 2024/10/23 16:50
*/
public class TextUtil {
public static String getShortStringBetweenKeys(String str, String first, String[] secondKeys) {
return getShortStringBetweenKeys(str, first, secondKeys, true);
}
public static String getShortStringBetweenLastKeys(String str, String first, String[] secondKeys) {
return getShortStringBetweenKeys(str, first, secondKeys, false);
}
public static String getShortStringBetweenKeys(String str, String first, String[] secondKeys, boolean asc) {
if(str != null && first != null && secondKeys != null) {
int firstIndex = -1;
if(asc) {
firstIndex = str.indexOf(first);
} else {
firstIndex = str.lastIndexOf(first);
}
if(firstIndex != -1) {
int secondIndex = -1;
firstIndex += first.length();
for(int i = 0; i < secondKeys.length; i++) {
int index = str.indexOf(secondKeys[i], firstIndex);
if(index != -1 && (secondIndex == -1 || index < secondIndex)) {
secondIndex = index;
}
}
if(secondIndex == -1) {
secondIndex = str.length();
}
str = str.substring(firstIndex, secondIndex);
}
}
return str;
}
public static String getStringBetweenKeys(String str, String first, String second) {
if(str != null && first != null && second != null) {
int firstIndex = str.indexOf(first);
if(firstIndex != -1) {
firstIndex += first.length();
int secondIndex = str.indexOf(second, firstIndex);
if(secondIndex == -1) {
secondIndex = str.length();
}
str = str.substring(firstIndex, secondIndex);
} else {
str = "";
}
}
return str;
}
public static String getStringBetweenLastKeys(String str, String first, String second) {
if(str != null && first != null && second != null) {
int firstIndex = str.lastIndexOf(first);
if(firstIndex != -1 && first.equals(second)) {
firstIndex = str.lastIndexOf(first, firstIndex - first.length());
}
if(firstIndex != -1) {
firstIndex += first.length();
int secondIndex = str.indexOf(second, firstIndex);
if(secondIndex == -1) {
secondIndex = str.length();
}
str = str.substring(firstIndex, secondIndex);
}
}
return str;
}
public static String getStringAfterKey(String str, String key) {
return getStringAfterKey(str, key, 0);
}
public static String getStringAfterKey(String str, String key, int fromIndex) {
if(str != null && key != null) {
str = str.substring(str.indexOf(key, fromIndex) + key.length());
}
return str;
}
public static String getStringAfterLastKey(String str, String key) {
if(str != null && key != null) {
str = str.substring(str.lastIndexOf(key) + key.length());
}
return str;
}
public static String getStringBeforeKey(String str, String key) {
if(str != null && key != null && str.indexOf(key) != -1) {
str = str.substring(0, str.indexOf(key));
}
return str;
}
public static String getStringBeforeKeys(String str, String ... keys) {
int firstIndex = -1;
for(int i = 0; i < keys.length; i++) {
int index = str.indexOf(keys[i]);
if(index != -1 && (firstIndex == -1 || index < firstIndex)) {
firstIndex = index;
}
}
if(firstIndex == -1) {
firstIndex = str.length();
}
str = str.substring(0, firstIndex);
return str;
}
public static String getStringBeforeLastKey(String str, String key) {
if(str != null && key != null && str.lastIndexOf(key) != -1) {
str = str.substring(0, str.indexOf(key));
}
return str;
}
}
电话脱敏:
/**
* 电话脱敏
* @param phoneNumber 电话号码
* @return ignore
*/
private String maskPhoneNumber(String phoneNumber) {
if (phoneNumber == null || phoneNumber.length() < 8) {
return phoneNumber;
}
return phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
}
end
2025-03-19 start:
将一个时间片分片(每10分钟一个分片)
/**
* 将时间段拆分为 10 分钟一段的时间片
*
* @param startTimeStr 开始时间字符串(格式:yyyy-MM-dd HH:mm:ss)
* @param endTimeStr 结束时间字符串(格式:yyyy-MM-dd HH:mm:ss)
* @return 拆分后的时间片列表,每个时间片是一个 Map
*/
public static List<Map<String, String>> splitTimeRange(String startTimeStr, String endTimeStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将字符串解析为 LocalDateTime
LocalDateTime startTime = LocalDateTime.parse(startTimeStr, formatter);
LocalDateTime endTime = LocalDateTime.parse(endTimeStr, formatter);
List<Map<String, String>> timeRanges = new ArrayList<>();
// 如果开始时间大于结束时间,直接返回空列表
if (startTime.isAfter(endTime)) {
throw new IllegalArgumentException("开始时间不能晚于结束时间");
}
LocalDateTime currentStart = startTime;
// 循环拆分时间段
while (currentStart.isBefore(endTime)) {
LocalDateTime currentEnd = currentStart.plusMinutes(10);
if (currentEnd.isAfter(endTime)) {
currentEnd = endTime;
}
Map<String, String> timeRange = new HashMap<>();
timeRange.put("startTime", currentStart.format(formatter));
timeRange.put("endTime", currentEnd.format(formatter));
timeRanges.add(timeRange);
currentStart = currentEnd;
}
return timeRanges;
}
时间校验:
// 定义时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 解析输入的时间字符串为LocalDateTime对象
LocalDateTime startTimeDate = LocalDateTime.parse(startTime, formatter);
LocalDateTime endTimeDate = LocalDateTime.parse(endTime, formatter);
// 判断开始时间是否小于结束时间
if (startTimeDate.isAfter(endTimeDate) || startTimeDate.isEqual(endTimeDate)) {
throw new Exception("开始时间要小于结束时间");
}
// 计算时间差
Duration duration = Duration.between(startTimeDate, endTimeDate);
// 判断是否超过一个小时
if (duration.getSeconds() > 3600) {
throw new Exception("输入的时间不能超过一个小时");
}
end