加一些常量,代码优化

This commit is contained in:
thinkgem
2023-06-09 19:59:36 +08:00
parent 64195c8772
commit 7fe1faf915
2 changed files with 28 additions and 26 deletions

View File

@@ -4,14 +4,13 @@
*/ */
package com.jeesite.common.lang; package com.jeesite.common.lang;
import org.apache.commons.lang3.time.FastDateFormat;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.text.ParseException; import java.text.ParseException;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
/** /**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
* @author ThinkGem * @author ThinkGem
@@ -272,7 +271,6 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
/** /**
* 获取服务器启动时间 * 获取服务器启动时间
* @param date
* @return * @return
*/ */
public static Date getServerStartDate(){ public static Date getServerStartDate(){
@@ -305,7 +303,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
public static Date[] parseDateBetweenString(String dateString){ public static Date[] parseDateBetweenString(String dateString){
Date beginDate = null; Date endDate = null; Date beginDate = null; Date endDate = null;
if (StringUtils.isNotBlank(dateString)){ if (StringUtils.isNotBlank(dateString)){
String[] ss = StringUtils.split(dateString, "~"); String[] ss = StringUtils.split(dateString, StringUtils.TILDE);
if (ss != null && ss.length == 2){ if (ss != null && ss.length == 2){
String begin = StringUtils.trim(ss[0]); String begin = StringUtils.trim(ss[0]);
String end = StringUtils.trim(ss[1]); String end = StringUtils.trim(ss[1]);

View File

@@ -8,6 +8,7 @@ import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.collect.ListUtils; import com.jeesite.common.collect.ListUtils;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@@ -20,7 +21,19 @@ import java.util.regex.Pattern;
*/ */
public class StringUtils extends org.apache.commons.lang3.StringUtils { public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final char SEPARATOR = '_'; public static final String COMMA = ",";
public static final String COLON = ":";
public static final String TILDE = "~";
public static final String UNDERLINE = "_";
/**
* 分隔字符串(默认逗号分隔)
* @param str
* @return
*/
public static String[] split(final String str) {
return split(str, COMMA);
}
/** /**
* 转换为字节数组 * 转换为字节数组
@@ -29,28 +42,20 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*/ */
public static byte[] getBytes(String str){ public static byte[] getBytes(String str){
if (str != null){ if (str != null){
try { return str.getBytes(StandardCharsets.UTF_8);
return str.getBytes(EncodeUtils.UTF_8); }else{
} catch (UnsupportedEncodingException e) {
return null;
}
}else{
return null; return null;
} }
} }
/** /**
* 转换为字节数组 * 转换为字节数组
* @param str * @param bytes
* @return * @return
*/ */
public static String toString(byte[] bytes){ public static String toString(byte[] bytes){
try { return new String(bytes, StandardCharsets.UTF_8);
return new String(bytes, EncodeUtils.UTF_8); }
} catch (UnsupportedEncodingException e) {
return EMPTY;
}
}
/** /**
* 是否包含字符串 * 是否包含字符串
@@ -260,8 +265,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
boolean upperCase = false; boolean upperCase = false;
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i); char c = s.charAt(i);
if (c == UNDERLINE.charAt(0)) {
if (c == SEPARATOR) {
upperCase = i != 1; // 不允许第二个字符是大写 upperCase = i != 1; // 不允许第二个字符是大写
} else if (upperCase) { } else if (upperCase) {
sb.append(Character.toUpperCase(c)); sb.append(Character.toUpperCase(c));
@@ -309,7 +313,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
} }
if ((i > 0) && Character.isUpperCase(c)) { if ((i > 0) && Character.isUpperCase(c)) {
if (!upperCase || !nextUpperCase) { if (!upperCase || !nextUpperCase) {
sb.append(SEPARATOR); sb.append(UNDERLINE);
} }
upperCase = true; upperCase = true;
} else { } else {
@@ -362,7 +366,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* @return * @return
*/ */
public static String getRandomNum(int count) { public static String getRandomNum(int count) {
char[] codeSeq = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char[] codeSeq = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Random random = new Random(); Random random = new Random();
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@@ -384,7 +388,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*/ */
public static String getTreeNodeName(String isShowCode, String code, String name) { public static String getTreeNodeName(String isShowCode, String code, String name) {
if ("true".equals(isShowCode) || "1".equals(isShowCode)) { if ("true".equals(isShowCode) || "1".equals(isShowCode)) {
return "(" + code + ") " + StringUtils.replace(name, " ", ""); return "(" + code + ") " + StringUtils.replace(name, SPACE, EMPTY);
} else if ("2".equals(isShowCode)) { } else if ("2".equals(isShowCode)) {
return name/*StringUtils.replace(name, " ", "")*/ + " (" + code + ")"; return name/*StringUtils.replace(name, " ", "")*/ + " (" + code + ")";
} else { } else {