207 lines
7.1 KiB
Java
Raw Normal View History

2025-07-23 14:19:57 +08:00
package com.czlis.interfaceCommon.utils;
import java.io.UnsupportedEncodingException;
2025-07-23 14:19:57 +08:00
import java.util.Date;
import java.util.Map;
public class LisUtil {
/**
* 生日格式为:yyyy-MM-dd
* 根据出生日期计算年龄
* @param birthday
* @return
*/
public static Map<String,String> getAgeFromBirthDay(String birthday){
return AgeUtils.getAgeFromBirthDay(birthday);
2025-07-23 14:19:57 +08:00
}
/**
* 根据年龄获取出生日期
* @param nl
* @return
*/
public static Date getBirthDayFromAge(int nl, String nldw) {
return AgeUtils.getBirthDayFromAge(nl,nldw);
2025-07-23 14:19:57 +08:00
}
/**
* 按GBK编码的字节宽度截取字符串,最多取cutlens个字节(避免截取半个汉字)
* @param str 原始字符串
* @param beginidx 起始位置
* @param cutlens 截取长度
* @return 截取后的字符串
*/
public static String substringByGbkWidth(String str,int beginidx,int cutlens) {
if (str == null || str.isEmpty()) {
return "";
}
int targetWidth = cutlens; // 目标字节宽度
int currentWidth =beginidx; // 当前累计字节宽度
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
// 计算当前字符在GBK编码下的字节数
int charWidth;
try {
// GBK编码中,汉字占2字节,英文等占1字节
charWidth = String.valueOf(c).getBytes("GBK").length;
} catch (UnsupportedEncodingException e) {
// 理论上GBK是Java默认支持的编码,不会走到这里
throw new RuntimeException("不支持GBK编码", e);
}
// 判断加上当前字符后是否超过目标宽度
if (currentWidth + charWidth > targetWidth) {
break; // 超过则停止,不截取当前字符
}
// 未超过则累加宽度并添加字符
currentWidth += charWidth;
result.append(c);
// 如果刚好达到目标宽度,提前结束
if (currentWidth == targetWidth) {
break;
}
}
return result.toString();
}
2025-08-15 19:57:36 +08:00
/**
* 使用正则表达式判断字符串是否为数字
* 支持:整数(123)、小数(123.45)、正负号(-123、+45.6)
*/
public static boolean isNumber(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
// 正则表达式:匹配正负整数、正负小数
String regex = "^[+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)$";
return str.matches(regex);
}
/**
* 使用正则表达式判断字符串是否为数字
* 支持:整数(123)、小数(123.45),不包括带正负号的情况
*/
public static boolean isTureNumber(String str) {
if (str == null || str.trim().isEmpty()) {
return false;
}
// 正则表达式:仅匹配正整数、正小数(不包含正负号)
// 说明:
// ^[0-9]+(\.[0-9]*)?$ 匹配整数(如123)或带整数部分的小数(如123.45)
// |\.([0-9]+)$ 匹配纯小数(如.67)
String regex = "^[0-9]+(\\.[0-9]*)?|\\.[0-9]+$";
return str.matches(regex);
}
/**
* 将包含比较运算符的字符串转换为特定格式的数值字符串
* 不支持的格式或非数字内容将返回原值
* @param input 输入字符串,如">=123", ">123", "<=123", "<123"
* @return 转换后的字符串或原值
*/
public static String getOverNumber(String input) {
// 输入为空时返回原值
if (input == null || input.trim().isEmpty()) {
return input;
}
// 定义支持的运算符,按长度排序以便正确识别(先长后短)
String[] operators = {">=", "<=", ">", "<"};
String operator = null;
String numberPart = null;
// 识别运算符和数字部分
for (String op : operators) {
if (input.startsWith(op)) {
operator = op;
numberPart = input.substring(op.length());
break;
}
}
// 未识别到支持的运算符,返回原值
if (operator == null) {
return input;
}
// 尝试将数字部分转换为double
double number;
try {
number = Double.parseDouble(numberPart);
} catch (NumberFormatException e) {
// 数字部分格式不正确,返回原值
return input;
}
// 根据运算符进行转换
switch (operator) {
case ">=":
case ">":
return String.format("%.4f", number + 0.0001);
case "<=":
case "<":
return String.format("%.4f", number - 0.0001);
default:
return input;
}
}
/**
* 保留指定小数位数
* 支持比较符号(<=、>=、<、>)和负号(-)同时存在的情况
* @param input 输入字符串,如"123.1234"、">123.252"、"<=123.252"、"-45.67"、">=-89.01"
* @param decimalPlaces 要保留的小数位数,需为非负整数
* @return 格式化后的字符串,无法处理时返回原值
*/
public static String round(String input, int decimalPlaces) {
// 处理空输入或无效小数位数
if (input == null || input.trim().isEmpty() || decimalPlaces < 0) {
return input;
}
// 定义比较运算符前缀(按长度排序,确保正确识别)
String[] comparisonPrefixes = {"<=", ">=", "<", ">"};
String comparisonPrefix = "";
String remainingPart = input;
// 提取比较运算符前缀
for (String prefix : comparisonPrefixes) {
if (remainingPart.startsWith(prefix)) {
comparisonPrefix = prefix;
remainingPart = remainingPart.substring(prefix.length());
break;
}
}
// 尝试解析剩余部分为数字
try {
// 解析数字(包含可能的负号)
double number = Double.parseDouble(remainingPart);
// 创建格式化模式(整数部分至少1位,小数部分固定位数)
StringBuilder formatPattern = new StringBuilder();
formatPattern.append("0");
if (decimalPlaces > 0) {
formatPattern.append(".");
// 循环添加指定数量的0
for (int i = 0; i < decimalPlaces; i++) {
formatPattern.append("0");
}
}
// 格式化数字并拼接比较前缀
String formattedNumber = String.format(
"%s%s",
comparisonPrefix,
String.format(formatPattern.toString(), number)
);
return formattedNumber;
} catch (NumberFormatException e) {
// 数字部分无法解析时返回原值
return input;
}
}
2025-07-23 14:19:57 +08:00
}