package zj.java.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import zj.check.util.CheckUtil;
import zj.common.exception.ServiceException;
import zj.date.util.DateUtil;
import zj.type.TypeUtil;
/**
* java工具类
*
* @version 1.00 (2014.09.15)
* @author SHNKCS 张军 {@link <a target=_blank href="http://www.shanghaijiadun.com">上海加盾信息科技有限公司</a> <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>}
*/
public class JavaUtil implements Serializable {
private static final long serialVersionUID = 1L;
private transient static final Logger log = Logger.getLogger(JavaUtil.class);
/**
* 单位进位,中文默认为4位即(万、亿)
*/
public static int UNIT_STEP = 4;
/**
* 单位
*/
public static String[] CN_UNITS = new String[] { "个", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万" };
/**
* 汉字
*/
public static String[] CN_CHARS = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
/**
* 打印json
*
* @param json
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> parseMap(String json) {
return parseJson(json, Map.class);
}
/**
* 打印json
*
* @param json
*/
public static <T> T parseJson(String json, Class<T> clz) {
T result = JSON.parseObject(json, clz);
log.debug("json返回结果------------>");
log.debug(JSON.toJSONString(result, true));
return result;
}
/**
* 获取系统换行符号
*
* @return
*/
public static String getLineBreak() {
if (JavaUtil.isWindows()) {
return "\r\n";
} else if (JavaUtil.isMac()) {
return "\r";
} else {
return "\n";
}
}
/**
* 分割email地址,转换成一行
*
* @param p_emails
* email地址
*/
public static String emailSplitToOneline(String p_emails) {
return emailSplitToOneline(p_emails, ";");
}
/**
* 分割email地址,转换成一行
*
* @param p_emails
* email地址
*/
public static String emailSplitToOneline(String p_emails, String split) {
// String s = "a; bbcdd55 \r\nb\r\nc\r\nd";
String[] v_emails1 = JavaUtil.split(p_emails, getLineBreak());
String r_emails = "";
for (String v_email1 : v_emails1) {
v_email1 = JavaUtil.trim(v_email1);
if (CheckUtil.isNull(v_email1)) {
continue;
}
String[] v_emails2 = JavaUtil.split(v_email1, split);
for (String v_email2 : v_emails2) {
v_email2 = JavaUtil.trim(v_email2);
if (CheckUtil.isNull(v_email1)) {
continue;
}
if (CheckUtil.isNotNull(r_emails)) {
r_emails += split;
}
r_emails += v_email2;
}
}
return r_emails;
}
/**
* 是否是windows
*
* @return
*/
public static boolean isWindows() {
try {
String osName = System.getProperty("os.name");
return JavaUtil.toLowerCase(osName).indexOf("windows") != -1;
} catch (Exception e) {
return true;
}
}
/**
* 是否是mac系统
*
* @return
*/
public static boolean isMac() {
try {
String osName = System.getProperty("os.name");
return JavaUtil.toLowerCase(osName).indexOf("mac") != -1;
} catch (Exception e) {
return true;
}
}
/**
* 支持到12位
*
* @param numberStr
* 中文数字
* @return int 数字
*/
public static int getIntegerByStr(String numberStr) {
// 返回结果
int sum = 0;
// null或空串直接返回
if (numberStr == null || ("").equals(numberStr)) {
return sum;
}
// 过亿的数字处理
if (numberStr.indexOf("亿") > 0) {
String currentNumberStr = numberStr.substring(0, numberStr.indexOf("亿"));
int currentNumber = doByStr(currentNumberStr);
sum += currentNumber * Math.pow(10, 8);
numberStr = numberStr.substring(numberStr.indexOf("亿") + 1);
}
// 过万的数字处理
if (numberStr.indexOf("万") > 0) {
String currentNumberStr = numberStr.substring(0, numberStr.indexOf("万"));
int currentNumber = doByStr(currentNumberStr);
sum += currentNumber * Math.pow(10, 4);
numberStr = numberStr.substring(numberStr.indexOf("万") + 1);
}
// 小于万的数字处理
if (!("").equals(numberStr)) {
int currentNumber = doByStr(numberStr);
sum += currentNumber;
}
return sum;
}
/**
* 把亿、万分开每4位一个单元,解析并获取到数据
*
* @param testNumber
* @return
*/
public static int doByStr(String testNumber) {
// 返回结果
int sum = 0;
// null或空串直接返回
if (testNumber == null || ("").equals(testNumber)) {
return sum;
}
// 获取到千位数
if (testNumber.indexOf("千") > 0) {
String currentNumberStr = testNumber.substring(0, testNumber.indexOf("千"));
sum += doByStrReplace(currentNumberStr) * Math.pow(10, 3);
testNumber = testNumber.substring(testNumber.indexOf("千") + 1);
}
// 获取到百位数
if (testNumber.indexOf("百") > 0) {
String currentNumberStr = testNumber.substring(0, testNumber.indexOf("百"));
sum += doByStrReplace(currentNumberStr) * Math.pow(10, 2);
testNumber = testNumber.substring(testNumber.indexOf("百") + 1);
}
// 对于特殊情况处理 比如10-19是个数字,十五转化为一十五,然后再进行处理
if (testNumber.indexOf("十") == 0) {
testNumber = "一" + testNumber;
}
// 获取到十位数
if (testNumber.indexOf("十") > 0) {
String currentNumberStr = testNumber.substring(0, testNumber.indexOf("十"));
sum += doByStrReplace(currentNumberStr) * Math.pow(10, 1);
testNumber = testNumber.substring(testNumber.indexOf("十") + 1);
}
// 获取到个位数
if (!("").equals(testNumber)) {
sum += doByStrReplace(testNumber.replaceAll("零", ""));
}
return sum;
}
public static int doByStrReplace(String replaceNumber) {
switch (replaceNumber) {
case "一":
return 1;
case "二":
return 2;
case "三":
return 3;
case "四":
return 4;
case "五":
return 5;
case "六":
return 6;
case "七":
return 7;
case "八":
return 8;
case "九":
return 9;
case "零":
return 0;
default:
throw new ServiceException("无法解析大写数字【" + replaceNumber + "】");
}
}
/**
* 数字转大写,仅仅转大写
*
* @param pnum
* 数字字符串,可带其它字符
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String numToCnChar(String pnum) {
char[] cnumAry = pnum.toCharArray();
String snumNew = "";
for (char cnum : cnumAry) {
String snum = objToStr(cnum);
if (CheckUtil.isPlusNum(snum)) {
snumNew += CN_CHARS[TypeUtil.Primitive.intValue(snum)];
} else {
snumNew += snum;
}
}
return snumNew;
}
/**
* 数字转大写日期(只支持4位数字年或2位数字月日)
*
* @see 年、月、日分别调用
* @param pnum
* 数字
* @param decimal
* 是否是十进位,默认false,如果是true,则12-》十二
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String numToCnCharDate(int pnum) {
String rnum = numToCnChar(JavaUtil.objToStr(pnum));
if (pnum < 1000) {
if (pnum == 10) {
rnum = "十";
} else if (pnum > 10) {
if (rnum.startsWith("一")) {
rnum = "十" + rnum.substring(1);
} else {
rnum = rnum.substring(0, 1) + "十" + rnum.substring(1);
}
}
}
return rnum;
}
/**
* 数值转换为中文字符串 如2转化为贰
*/
public static String numToCn(long num) {
return numToCn(num, true);
}
/**
* 数值转换为中文字符串(口语化)
*
* @param num
* 需要转换的数值
* @param isColloquial
* 是否口语化。例如12转换为'十二'而不是'一十二'。
* @return
*/
public static String numToCn(String num, boolean isColloquial) {
int integer = 0, decimal = 0;
StringBuffer strs = new StringBuffer(32);
String[] splitNum = num.split("\\.");
if (splitNum.length > 0)
integer = Integer.parseInt(splitNum[0]); // 整数部分
if (splitNum.length > 1)
decimal = Integer.parseInt(splitNum[1]); // 小数部分
String[] result_1 = numConvert(integer, isColloquial);
for (String str1 : result_1)
strs.append(str1);
if (decimal == 0) {// 小数部分为0时
return strs.toString();
} else {
String result_2 = numToCnChar("" + decimal); // 例如5.32,小数部分展示三二,而不是三十二
strs.append("点");
strs.append(result_2);
return strs.toString();
}
}
/**
* 对于int,long类型的数据处理
*
* @param num
* 需要转换的数值
* @param isColloquial
* 是否口语化。例如12转换为'十二'而不是'一十二'。
* @return
*/
public static String numToCn(long num, boolean isColloquial) {
String[] result = numConvert(num, isColloquial);
StringBuffer strs = new StringBuffer(32);
for (String str : result) {
strs.append(str);
}
return strs.toString();
}
/**
* 将数值转换为中文
*
* @param num
* 需要转换的数值
* @param isColloquial
* 是否口语化。例如12转换为'十二'而不是'一十二'。
* @return
*/
public static String[] numConvert(long num, boolean isColloquial) {
if (num < 10) {// 10以下直接返回对应汉字
return new String[] { CN_CHARS[(int) num] };// ASCII2int
}
char[] chars = String.valueOf(num).toCharArray();
if (chars.length > CN_UNITS.length) {// 超过单位表示范围的返回空
return new String[] {};
}
boolean isLastUnitStep = false;// 记录上次单位进位
ArrayList<String> cnchars = new ArrayList<String>(chars.length * 2);// 创建数组,将数字填入单位对应的位置
for (int pos = chars.length - 1; pos >= 0; pos--) {// 从低位向高位循环
char ch = chars[pos];
String cnChar = CN_CHARS[ch - '0'];// ascii2int 汉字
int unitPos = chars.length - pos - 1;// 对应的单位坐标
String cnUnit = CN_UNITS[unitPos];// 单位
boolean isZero = (ch == '0');// 是否为0
boolean isZeroLow = (pos + 1 < chars.length && chars[pos + 1] == '0');// 是否低位为0
boolean isUnitStep = (unitPos >= UNIT_STEP && (unitPos % UNIT_STEP == 0));// 当前位是否需要单位进位
if (isUnitStep && isLastUnitStep) {// 去除相邻的上一个单位进位
int size = cnchars.size();
cnchars.remove(size - 1);
if (!CN_CHARS[0].equals(cnchars.get(size - 2))) {// 补0
cnchars.add(CN_CHARS[0]);
}
}
if (isUnitStep || !isZero) {// 单位进位(万、亿),或者非0时加上单位
cnchars.add(cnUnit);
isLastUnitStep = isUnitStep;
}
if (isZero && (isZeroLow || isUnitStep)) {// 当前位为0低位为0,或者当前位为0并且为单位进位时进行省略
continue;
}
cnchars.add(cnChar);
isLastUnitStep = false;
}
Collections.reverse(cnchars);
// 清除最后一位的0
int chSize = cnchars.size();
String chEnd = cnchars.get(chSize - 1);
if (CN_CHARS[0].equals(chEnd) || CN_UNITS[0].equals(chEnd)) {
cnchars.remove(chSize - 1);
}
// 口语化处理
if (isColloquial) {
String chFirst = cnchars.get(0);
String chSecond = cnchars.get(1);
if (chFirst.equals(CN_CHARS[1]) && chSecond.startsWith(CN_UNITS[1])) {// 是否以'一'开头,紧跟'十'
cnchars.remove(0);
}
}
return cnchars.toArray(new String[] {});
}
/**
* 检查数据差异
*
* @param text
* 数字
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static <T> List<T> diffColl(Collection<T> c1, Collection<T> c2) {
List<T> result = new ArrayList<T>();
try {
// 添加至去重列表中
Set<T> allC = new HashSet<T>();
allC.addAll(c1);
allC.addAll(c2);
for (T v : allC) {
if (c1.contains(v) && c2.contains(v)) {
// 两个集合中的数据共同包含
} else {
// 两个集合中的数据不相同的
result.add(v);
}
}
} catch (Exception e) {
throw new ServiceException(e);
}
return result;
}
/**
* DecimalFormat转换数字
*
* @param text
* 数字
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String covertNumEToString(Object text) {
return covertNumEToString(text, null);
}
/**
* DecimalFormat转换数字
*
* @param text
* 数字
* @param format
* 格式
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String covertNumEToString(Object text, String format) {
try {
if (CheckUtil.isNull(format)) {
format = "#.##";
}
if (!(text instanceof String)) {
DecimalFormat df = new DecimalFormat(format);
Double d = Double.parseDouble(df.format(text));
String sd = df.format(d);
return sd;
}
return objToStr(text);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* DecimalFormat转换数字
*
* @param text
* 数字
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String covertToNumSplit(Object text) {
return covertToNumSplit(text, null);
}
/**
* DecimalFormat转换数字
*
* @param text
* 数字
* @param format
* 格式
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String covertToNumSplit(Object text, String format) {
try {
if (CheckUtil.isNull(format)) {
format = "###,###.##";
}
DecimalFormat df = new DecimalFormat(format);
return df.format(TypeUtil.Primitive.doubleValueNoCatchError(text));
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 转换数字
*
* @param text
* 数字
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static BigDecimal covertBigDecimal(Object text) {
String value = JavaUtil.conversionStringToNumber(JavaUtil.trim(JavaUtil.objToStr(text)));
BigDecimal bvalue = BigDecimal.ZERO;
if (CheckUtil.isNotNull(value)) {
try {
bvalue = new BigDecimal(value);
} catch (Exception e) {
// 不做处理
}
}
return bvalue;
}
/**
* 获取in的字符串
*
* @param strs
* 字符集合
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String sqlInStr(Collection<String> strs) {
String rtnStrs = "";
int i = 0;
for (String s : strs) {
if (i != 0)
rtnStrs += ",";
rtnStrs += "'" + s + "'";
i++;
}
return rtnStrs;
}
/**
* 转换文件大小为B、KB、MB、GB
*
* @param size
* 文件大小
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String convertFileSize(long size) {
// 转成大数据
if (size < 1024) {
// B
return String.valueOf(size) + "B";
} else {
// KB
BigDecimal b1024 = new BigDecimal(1024);
BigDecimal tempSize = new BigDecimal(size);
BigDecimal valueSize = tempSize.divide(b1024, 8, BigDecimal.ROUND_HALF_UP);
if (valueSize.compareTo(b1024) <= 0) {
// 如果小于1024
return valueSize.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "KB";
} else {
// MB
valueSize = valueSize.divide(b1024, 6, BigDecimal.ROUND_HALF_UP);
if (valueSize.compareTo(b1024) <= 0) {
// 如果小于1024
return valueSize.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "MB";
} else {
// GB
valueSize = valueSize.divide(b1024, 4, BigDecimal.ROUND_HALF_UP);
if (valueSize.compareTo(b1024) <= 0) {
// 如果小于1024
return valueSize.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "GB";
} else {
// TB
valueSize = valueSize.divide(b1024, 2, BigDecimal.ROUND_HALF_UP);
if (valueSize.compareTo(b1024) <= 0) {
// 如果小于1024
return valueSize.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "TB";
} else {
// 默认
return tempSize.toString() + "B";
}
}
}
}
}
}
/**
* 深度拷贝list数据
*
* @param src
* 源集合
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<T> deepCopy(List<T> src) {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
return (List<T>) in.readObject();
} catch (Exception e) {
return null;
}
}
/**
* 合并两个数组
*
* @param first
* 第一个数组
* @param second
* 第二个数组
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static <T> T[] concatArray(T[] first, T[] second) {
T[] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
// @SuppressWarnings("unchecked")
// public static <T> T[] concatArray(T[] a, T[] b) {
// final int alen = a.length;
// final int blen = b.length;
// if (alen == 0) {
// return b;
// }
// if (blen == 0) {
// return a;
// }
// final T[] result = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), alen + blen);
// System.arraycopy(a, 0, result, 0, alen);
// System.arraycopy(b, 0, result, alen, blen);
// return result;
// }
/**
* 合并多个数组
*
* @param first
* 第一个数组
* @param rest
* 多个组合
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T[] concatArrayAll(T[] first, T[]... rest) {
int totalLength = first.length;
for (T[] array : rest) {
totalLength += array.length;
}
T[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (T[] array : rest) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;
}
/**
* 替换字符串
*
* @param template
* 模板字符串
* @param placeholder
* @param replacement
* @return
*/
public static String replaceOnce(String template, String placeholder, String replacement) {
// System.out.println("varchar($2)".replaceAll("\\$2", "9")+replaceOnce("varchar($2)","$2","6"));
int loc = template == null ? -1 : template.indexOf(placeholder);
if (loc < 0) {
return template;
} else {
return new StringBuffer(template.substring(0, loc)).append(replacement).append(template.substring(loc + placeholder.length())).toString();
}
}
/**
* 去除text所有空格
*
* @param text
* @return
*/
public static final String trims(String text) {
if (CheckUtil.isNull(text)) {
return text;
}
String newStr = "";
char[] chars = text.toCharArray();
for (char c : chars) {
String s = JavaUtil.objToStr(c);
if (CheckUtil.isNull(s)) {
continue;
}
newStr += s;
}
return newStr;
}
/**
* 去除text左右空格
*
* @param text
* @return
*/
public static final String trim(String text) {
return ltrim(rtrim(text));
}
/**
* 替换左字符 String d = "b d a e abcd f "; String rs = CommonUtil.ltrimText(d, "a", "b", " d");
*
* @param text
* @param trimText
* @return
*/
public static final String ltrimText(final String text, String... trimText) {
if (text == null) {
return null;
}
if (trimText == null || trimText.length == 0) {
return ltrim(text);
}
int pos = 0;
for (; pos < text.length(); pos++) {
boolean notTrimChar = true;
for (int i = 0; i < trimText.length; i++) {
notTrimChar &= (trimText[i].indexOf(text.charAt(pos)) < 0);
}
if (notTrimChar) {
break;
}
}
return text.substring(pos);
}
/**
* 替换右字符
*
* @param text
* @param trimText
* @return
*/
public static final String rtrimText(final String text, String... trimText) {
if (text == null) {
return null;
}
if (trimText == null || trimText.length == 0) {
return rtrim(text);
}
int pos = text.length() - 1;
for (; pos >= 0; pos--) {
boolean notTrimChar = true;
for (int i = 0; i < trimText.length; i++) {
notTrimChar &= (trimText[i].indexOf(text.charAt(pos)) < 0);
}
if (notTrimChar) {
break;
}
}
return text.substring(0, pos + 1);
}
/**
* 替换左字符串
*
* @param text
* @return
*/
public static final String ltrim(String text) {
return ltrim(text, null);
}
/**
* 替换左字符串
*
* @param text
* @param trimText
* @return
*/
public static final String ltrim(String text, String trimText) {
if (text == null)
return null;
if (trimText == null)
trimText = " ";
int pos;
for (pos = 0; pos < text.length() && trimText.indexOf(text.charAt(pos)) >= 0; pos++)
;
return text.substring(pos);
}
/**
* 替换右字符串
*
* @param text
* @return
*/
public static final String rtrim(String text) {
return rtrim(text, null);
}
/**
* 替换右字符串
*
* @param text
* @param trimText
* @return
*/
public static final String rtrim(String text, String trimText) {
if (text == null)
return null;
if (trimText == null)
trimText = " ";
int pos;
for (pos = text.length() - 1; pos >= 0 && trimText.indexOf(text.charAt(pos)) >= 0; pos--)
;
return text.substring(0, pos + 1);
}
/**
* 分割字符串类
*
* @author zhangjun
*
*/
public static class SplitConstants {
public static final String TRIM = "all";
public static final String NRIM = "none";
public static final String LTRIM = "left";
public static final String RTRIM = "right";
public static final String NO_CASE = "";
public static final String TO_UPPER_CASE = "0";
public static final String TO_LOWER_CASE = "1";
}
/**
* 分割字符串
*
* @param str
* @param delim
* @return
*/
public static final String[] split(String str, String delim) {
return split(str, delim, SplitConstants.TRIM, SplitConstants.NO_CASE);
}
/**
* 分割字符串
*
* @param str
* @param delim
* @param caseType
* @return
*/
public static final String[] split(String str, String delim, String caseType) {
return split(str, delim, SplitConstants.TRIM, caseType);
}
/**
* 分割字符串
*
* @param str
* @param delim
* @param trim
* 去除左右空格
* @param caseType
* @return
*/
public static final String[] split(String str, String delim, String trim, String caseType) {
String[] arrayStrings;
try {
arrayStrings = mySplit(str, delim, trim, caseType);
} catch (Throwable t) {
arrayStrings = null;
}
if (arrayStrings == null) {
arrayStrings = new String[0];
}
return arrayStrings;
}
/**
* 分割字符串
*
* @param s
* @param delimiter
* @param trim
* @param caseType
* @return
*/
private static String[] mySplit(String str, String delimiter, String trim, String caseType) {
int delimiterLength;
int stringLength = str.length();
if (delimiter == null || (delimiterLength = delimiter.length()) == 0) {
return new String[] { str };
}
int count;
int start;
int end;
count = 0;
start = 0;
while ((end = str.indexOf(delimiter, start)) != -1) {
count++;
start = end + delimiterLength;
}
count++;
String[] result = new String[count];
count = 0;
start = 0;
while ((end = str.indexOf(delimiter, start)) != -1) {
result[count] = getTrimStr(str.substring(start, end), trim, caseType);
count++;
start = end + delimiterLength;
}
end = stringLength;
result[count] = getTrimStr(str.substring(start, end), trim, caseType);
return (result);
}
/**
* 合并字符串
*
* @param str
* @param trim
* @param caseType
* @return
*/
private static String getTrimStr(String str, String trim, String caseType) {
if (SplitConstants.TRIM.equals(trim)) {
str = str.trim();
} else if (SplitConstants.LTRIM.equals(trim)) {
str = ltrim(str);
} else if (SplitConstants.RTRIM.equals(trim)) {
str = rtrim(str);
}
if (SplitConstants.TO_LOWER_CASE.equals(caseType)) {
str = str.toLowerCase();
} else if (SplitConstants.TO_UPPER_CASE.equals(caseType)) {
str = str.toUpperCase();
}
return str;
}
/**
* 转strs数组为字符串用split分割
*
* @param strs
* @param split
* @return
*/
public static String getAryStrs(String[] strs, String split) {
return getAryStrs(strs, split, "");
}
/**
* 转split分割的str字符串,转新的分割newSplit,添加在值左右添加addStr字符串
*
* @param str
* @param split
* @param newSplit
* @param addStr
* @return
*/
public static String getAryStrs(String str, String split, String newSplit, String addStr) {
return getAryStrs(split(str, split), newSplit, addStr);
}
/**
* 转strs数组为字符串用split分割,添加在值左右添加addStr字符串
*
* @param strs
* @param split
* @param addStr
* @return
*/
public static String getAryStrs(String[] strs, String split, String addStr) {
String rtnStrs = "";
try {
if (strs.length == 1 && strs[0].equals(""))
return "";
for (int i = 0; i < strs.length; i++) {
if (i != 0)
rtnStrs += split;
rtnStrs += addStr + strs[i] + addStr;
}
} catch (Exception e) {
rtnStrs = "";
log.error(e.getMessage());
}
return rtnStrs;
}
/**
* 返回String
*
* @param o
* @return
*/
public static String objToStr(Object obj) {
String str = "";
if (obj == null) {
return str;
} else {
if (obj instanceof Date) {
str = DateUtil.dateParse((Date) obj, "yyyy-MM-dd HH:mm:ss");
} else {
str = obj.toString();
}
}
return str;
}
/**
* 返回String
*
* @param o
* @return
*/
public static String objToStrNull(Object obj) {
if (obj == null) {
return null;
} else {
return objToStr(obj);
}
}
/**
* 转换成大写
*
* @param o
* @return
*/
public static String toUpperCase(String o) {
return o == null ? null : o.toUpperCase();
}
/**
* 转换成小写
*
* @param o
* @return
*/
public static String toLowerCase(String o) {
return o == null ? null : o.toLowerCase();
}
/**
* LIST转数组
*
* @param list
* @param clazz
* @return
*/
public static <T> T[] listToArray(List<T> list, Class<T> clazz) {
if (list == null || list.size() == 0)
return null;
@SuppressWarnings("unchecked")
T[] array = (T[]) Array.newInstance(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}
return array;
}
/**
* 获取最大长度文字
*
* @param text
* @param count
* @return
*/
public static String getMaxText(String text, int count) {
// 设置文章标题最大值
if (text == null || text.trim().equals(""))
return "";
int maxIndex = -1;
maxIndex = text.length();
if (maxIndex > count) {
return text.substring(0, count) + "...";
} else {
return text;
}
}
/**
* 转换重复的list集合
*
* @param list
* @return
*/
public static List<Map<Object, List<Object>>> convertRepeatList(List<Map<Object, Object>> list) {
List<Map<Object, List<Object>>> rtnList = new ArrayList<Map<Object, List<Object>>>();
Map<Object, Object> newMap = new HashMap<Object, Object>();
for (Map<Object, Object> map : list) {
Object key = map.keySet().iterator().next();
newMap.put(key, null);
}
Map<Object, List<Object>> newMaps = null;
List<Object> newLists = null;
for (Object key : newMap.keySet()) {
newMaps = new HashMap<Object, List<Object>>();
newLists = new ArrayList<Object>();
for (Map<Object, Object> map : list) {
Object value2 = map.get(key);
if (map.containsKey(key)) {
newLists.add(value2);
}
}
newMaps.put(key, newLists);
rtnList.add(newMaps);
}
return rtnList;
}
/**
* 获取UUID唯一标识
*
* @return
*/
public static synchronized String getUUID() {
// + "-" + System.currentTimeMillis()
return getUUID("");
}
/**
*
* @return
*/
public static synchronized String getUUID32() {
// + "-" + System.currentTimeMillis()
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 获取UUID唯一标识
*
* @return
*/
public static synchronized String getUUID(Class<?> cla) {
return getUUID(cla.getSimpleName());
}
/**
* 获取UUID唯一标识
*
* @return
*/
public static synchronized String getUUID(String prefix) {
String ymdhmss = DateUtil.dateParse(new Date(), "yyyyMMddHHmmssSSS");
return (CheckUtil.isNull(prefix) ? "id_" + ymdhmss : toLowerCase(prefix) + "_" + ymdhmss) + "_" + getUUID32();
}
/**
* Object对象换int类型
*
* @param o
* @return
*/
public static int getIntValue(Object o) {
if (o == null) {
return 0;
} else {
try {
if (o instanceof BigDecimal) {
return ((BigDecimal) o).intValue();
} else if (o instanceof Integer) {
return ((Integer) o).intValue();
} else if (o instanceof Long) {
return ((Long) o).intValue();
} else if (o instanceof String) {
return Integer.parseInt(String.valueOf(o));
} else {
return Integer.parseInt(o + "");
}
} catch (Exception e) {
log.error(e.getMessage());
return 0;
}
}
}
/**
* 去除数组中为null或""的值
*
* @param values
* @return
*/
public static String[] aryTrimValue(String[] values) {
if (values == null) {
return null;
} else {
List<String> valuesLst = new ArrayList<String>();
for (String value : values) {
if (CheckUtil.isNotNull(value)) {
valuesLst.add(value);
}
}
return (String[]) valuesLst.toArray(new String[valuesLst.size()]);
}
}
/**
* 提取字符串中的整数字
*
* @param txt
* @return
*/
public static int conversionStringToNum(String txt) {
try {
return Integer.parseInt(conversionStringToString("[^0-9]", txt));
} catch (Exception e) {
return 0;
}
}
/**
* 提取字符串中的数字
*
* @param txt
* @return
*/
public static String conversionStringToNumber(String txt) {
return conversionStringToString("[^0-9\\.-]", txt);
}
/**
* 提取字符串中的数字
*
* @param txt
* @return
*/
public static String conversionStringToString(String regEx, String txt) {
if (CheckUtil.isNull(txt))
return null;
// String regEx = "[^0-9]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(txt);
// 替换非数字字符
String str = m.replaceAll("").trim();
return str;
}
/**
* 取得布尔值
*
* @param txt
* @return
*/
@Deprecated
public static boolean getBoolean(String txt) {
try {
return Boolean.parseBoolean(txt);
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
/**
* 取得Long值
*
* @param txt
* @return
*/
@Deprecated
public static long getLong(String txt) {
try {
return Long.parseLong(txt);
} catch (Exception e) {
log.error(e.getMessage());
return 0l;
}
}
/**
* 获取批量集合值
*
* @param batchList
* 集合数据
* @param batchSize
* 每页显示条数
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return 批量集合值
*/
public static <T> List<List<T>> getBatchList(List<T> batchList, int batchSize) {
List<List<T>> batchLists = new ArrayList<List<T>>();
if (batchSize == 0) {
return batchLists;
}
List<T> tempBatchList = null;
int mod = batchList.size() % batchSize;
int remainder = batchList.size() / batchSize;
for (int i = 0; i < remainder; i++) {
tempBatchList = new ArrayList<T>();
for (int j = 1 + batchSize * i; j <= batchSize * (i + 1); j++) {
tempBatchList.add(batchList.get(j - 1));
}
batchLists.add(tempBatchList);
}
if (mod != 0) {
tempBatchList = new ArrayList<T>();
for (int i = batchSize * remainder; i < batchList.size(); i++) {
tempBatchList.add(batchList.get(i));
}
batchLists.add(tempBatchList);
}
return batchLists;
}
/**
* 获取固定批量集合
*
* @param batchList
* 集合数据
* @param batchSize
* 批量集合大小
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return 批量集合
*/
public static <T> List<List<T>> getFixedBatchList(List<T> batchList, int batchSize) {
List<List<T>> batchLists = new ArrayList<List<T>>();
if (batchSize == 0) {
return batchLists;
}
int count = batchList.size();
// 每个线程处理的集合数
int preCount = count / batchSize;
// 判断是否整除线程数
int modCount = count % batchSize;
if (modCount != 0) {
preCount++;
}
// 记录索引
int index = 0;
List<T> tempTask = new ArrayList<T>();
batchLists.add(tempTask);
for (int i = 0; i < count; i++) {
T value = batchList.get(i);
tempTask.add(value);
if (i != count - 1) {
// 如果非最后一个元素
index++;
// 如果等于计算出来的每页条数
if (index == preCount) {
// 实例化集合
tempTask = new ArrayList<T>();
batchLists.add(tempTask);
// 重新开始
index = 0;
}
}
}
return batchLists;
}
/**
* 根据页码条数计算多少页
*
* @param total
* 总条数
* @param rows
* 每页显示条数
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return 总页数
*/
public static int calcPageCount(int total, int rows) {
int pageCount = total / rows;
int mod = total % rows;
if (mod != 0) {
pageCount++;
}
return pageCount;
}
/**
* 根据总条数获取固定页数
*
* @param total
* 总条数
* @param rows
* 每页显示条数
* @param page
* 当前页
* @param showPageCount
* 显示多少页
*
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return 集合对象
* @see 开始页:map.get("startPage")
* @see 结束页:map.get("pageCount")
*/
public static Map<String, Object> calcPageCount(int total, int rows, int page, int showPageCount) {
int pageCount = calcPageCount(total, rows);
return calcPageCount(pageCount, page, showPageCount);
}
/**
* 根据总条数获取固定页数
*
* @param pageCount
* 总页码
* @param page
* 当前页
* @param showPageCount
* 显示多少页
*
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return 集合对象
* @see 开始页:map.get("startPage")
* @see 结束页:map.get("pageCount")
*/
public static Map<String, Object> calcPageCount(int pageCount, int page, int showPageCount) {
Map<String, Object> map = new HashMap<String, Object>();
// 最新开始页码值
int newStartPage = 1;
// 最新结束页码值
int newPageCount = pageCount;
// 计算左右显示的页码个数
int prePageCount = showPageCount / 2;
// 判断右边显示的页码个数
int modPageCount = showPageCount % 2;
// 右边多取个页码
int leftPageCount = prePageCount;
int rightPageCount = prePageCount;
if (modPageCount == 0) {
rightPageCount = rightPageCount - 1;
}
// 如果当前页码值大于指定的页码个数
if (page > prePageCount) {
// 右边页码个数的最后一个页码
int endPage = page + rightPageCount;
// 如果小于总页码个数
if (endPage < pageCount) {
// 赋值右边页码个数的最后一个页码
newPageCount = endPage;
}
// 左边页码个数的第一个页码
newStartPage = page - leftPageCount;
// 取得当前页面后的右边页码个数(如果rightPageCountCheck=0时,newStartPage=0,所以下面会有判断newStartPage是否小于等于0)
int rightPageCountCheck = newPageCount - page;
if (rightPageCountCheck < rightPageCount) {
// 判断右边页码个数没有达到和左边的页码个数一至
// 取得右边页码个数相差页码个数
int rightDiffPageCount = rightPageCount - rightPageCountCheck;
// 如果未达到,则当前页码前面的页码总数向前移动,使得页码达到11个页码
newStartPage = newStartPage - rightDiffPageCount;
}
} else {
if (pageCount > showPageCount - 1) {
// 如果页码个数大于10,则输出最大页码个数11个
newPageCount = showPageCount;
}
}
if (newStartPage <= 0) {
// 开始页码从1开始
newStartPage = 1;
}
// 设置返回值
map.put("startPage", newStartPage);
map.put("pageCount", newPageCount);
return map;
}
/**
* 获取最后分割符前面的字符串
*
* @param text
* @param split
* @return
*/
public static String getBeforeTextLastDelimiter(String text, String split) {
if (text == null)
return null;
if ("".equals(text))
return "";
int lastIndex = text.lastIndexOf(split);
if (lastIndex != -1) {
text = text.substring(0, lastIndex);
}
return text;
}
/**
* 去除小数点后的字符
*
* @param text
* @param split
* @return
*/
public static Object getIsNumValue(Object value) {
if (value == null)
return null;
if ("".equals(value))
return "";
String stext = value.toString();
int lastIndex = stext.lastIndexOf(".");
if (lastIndex == -1) {
return value;
}
String beforeText = getBeforeTextLastDelimiter(stext, ".");
String afterText = getAfterTextLastDelimiter(stext, ".");
// System.out.println(beforeText);
// System.out.println(afterText);
if (CheckUtil.isPlusNum(beforeText) && CheckUtil.isPlusNum(afterText)) {
if (BigDecimal.ZERO.compareTo(new BigDecimal(afterText)) == 0) {
return beforeText;
} else {
return value;
}
} else {
return value;
}
}
/**
* 获取最后分割符后面的字符串
*
* @param text
* @param split
* @return
*/
public static String getAfterTextLastDelimiter(String text, String split) {
if (text == null)
return null;
if ("".equals(text))
return "";
int lastIndex = text.lastIndexOf(split);
if (lastIndex != -1) {
text = text.substring(lastIndex + 1);
}
return text;
}
/**
* 获取map中的值为map对象
*
* @param map
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public static synchronized <K, V> Map<K, V> getValueForMap(Map<K, V> map, String key) {
Map<K, V> valueMap = null;
Object value = null;
if (map == null) {
valueMap = new HashMap<K, V>();
} else {
value = map.get(key);
valueMap = value instanceof Map ? (Map<K, V>) value : new HashMap<K, V>();
}
return valueMap;
}
/**
* 获取map中的值为list.map对象
*
* @param map
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public static synchronized <K, V> List<Map<K, V>> getValueForListMap(Map<K, V> map, String key) {
List<Map<K, V>> valueListMap = null;
Object value = null;
if (map == null) {
valueListMap = new ArrayList<Map<K, V>>();
} else {
value = map.get(key);
valueListMap = value instanceof List ? (List<Map<K, V>>) value : new ArrayList<Map<K, V>>();
}
return valueListMap;
}
/**
* 获取map中的值为list对象
*
* @param map
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public static synchronized <K, V, L> List<L> getValueForList(Map<K, V> map, String key) {
List<L> valueListMap = null;
Object value = null;
if (map == null) {
valueListMap = new ArrayList<L>();
} else {
value = map.get(key);
valueListMap = value instanceof List ? (List<L>) value : new ArrayList<L>();
}
return valueListMap;
}
/**
* list转set
*
* @param map
* @param key
* @return
*/
public static <T> Set<T> listToSet(List<T> P_coll) {
Set<T> R_coll = new HashSet<T>();
if (P_coll == null) {
return R_coll;
} else {
for (T V_t : P_coll) {
R_coll.add(V_t);
}
}
return R_coll;
}
/**
* set转list
*
* @param map
* @param key
* @return
*/
public static <T> List<T> setToList(Set<T> P_coll) {
List<T> R_coll = new ArrayList<T>();
if (P_coll == null) {
return R_coll;
} else {
for (T V_t : P_coll) {
R_coll.add(V_t);
}
}
return R_coll;
}
/**
* 判断两数组相同
*
* @param a1
* @param a2
* @return
*/
public static boolean arrayEquals(Object[] a1, Object[] a2) {
if (a1 == null) {
return a2 == null || a2.length == 0;
}
if (a2 == null) {
return a1.length == 0;
}
if (a1.length != a2.length) {
return false;
}
for (int i = 0; i < a1.length; i++) {
if (a1[i] != a2[i]) {
return false;
}
}
return true;
}
/**
* 判断两集合是否相同
*
* @param list1
* @param list2
* @return
*/
public static boolean collEquals(Collection<Object> list1, Collection<Object> list2) {
// 个数相同
boolean ok = true;
boolean isCheck = false;
quit: while (list1.size() != 0 && list1.size() == list2.size()) {
isCheck = true;
for (Object value : list1) {
if (list2.contains(value)) {
list2.remove(value);
list1.remove(value);
break;
} else {
ok = false;
break quit;
}
}
}
// if (isCheck && ok) {
// System.out.println("集合相同");
// } else {
// System.out.println("集合不相同");
// }
return isCheck && ok;
}
/**
* 判断两集合是否相同
*
* @param list1
* @param list2
* @return
*/
public static boolean collEqualsStr(Collection<String> list1, Collection<String> list2) {
List<Object> list1Obj = new ArrayList<Object>();
List<Object> list2Obj = new ArrayList<Object>();
for (String v : list1) {
list1Obj.add(v);
}
for (String v : list2) {
list2Obj.add(v);
}
return collEquals(list1Obj, list2Obj);
}
/**
* 首字母变小写
*
* @param text
* 字符串
* @return 以第一个字母小写其他不变
*/
public static String firstLowerCase(String text) {
return text.substring(0, 1).toLowerCase() + text.substring(1, text.length());
}
/**
* 首字母变大写
*
* @param text
* @return
*/
public static String firstUpperCase(String text) {
return text.substring(0, 1).toUpperCase() + text.substring(1, text.length());
}
/**
* 以分割符split首字母变大写(默认第一个字符不改变大写)
*
* @param text
* 校验字符串
* @param split
* 分割符
* @return 转换后的结果
*/
public static String upperCaseSplit(String text, String split) {
return upperCaseSplit(text, split, false);
}
/**
* 以分割符split首字母变大写
*
* @param text
* 校验字符串
* @param split
* 分割符
* @param first
* 是否首字母大写
* @return 转换后的结果
*/
public static String upperCaseSplit(String text, String split, boolean first) {
String[] texts = split(text, split);
String newText = "";
for (int i = 0; i < texts.length; i++) {
String str = texts[i];
if (CheckUtil.isNull(str)) {
continue;
}
if (i == 0) {
if (first) {
newText += firstUpperCase(str);
} else {
newText += str;
}
} else {
newText += firstUpperCase(str);
}
}
return newText;
}
/**
* 转换数据库字段为java属性
*
* @param text
* 数据库字段名
* @author 张军
* @date 2015-11-03 21:59:00
* @modifiyNote
* @version 1.0
* @return
*/
public static String convertDbToJavaField(String text) {
return JavaUtil.upperCaseSplit(JavaUtil.toLowerCase(text), "_");
}
/**
* 改变第一行bug BufferedReader.readLine()读取第一行会出现bug,首行第一个字符会是一个空字符 line = br.readLine(); line = readFirstLine(line); 文件保存为UTF-8格式,会出现此问题(例如:文件内容第一行以#号开头) stream/a.txt不正常,b.txt正常
*
* @param line
* @return
* @author zhangjun
*/
public static String readFirstLine(String line) {
if (line == null)
return null;
line = line.trim();
if ("".equals(line)) {
return line;
}
char s = line.charAt(0);
int hc = String.valueOf(s).hashCode();
if (hc == 65279) {
if (line.length() > 1) {
line = line.substring(1);
} else {
line = "";
}
}
return line;
}
/**
* 随机数
*
* @author zhangjun
*
*/
public static class RandomUtil {
private static Random RD = null;
static {
RD = new Random();
}
/**
* 获取[s-e]之间的随机数
*
* @param s
* 开始数字
* @param e
* 结束数字
* @return 随机数
*/
public static int getInt(int s, int e) {
return RD.nextInt(e - s + 1) + s;
}
}
/**
* 获取手机号去掉+86
*
* @param P_mobile
* 手机号
* @return
*/
public static String getMobile(String P_mobile) {
if (CheckUtil.isNull(P_mobile)) {
return "";
}
if (P_mobile.startsWith("+86")) {
// 去除
P_mobile = P_mobile.substring(3);
}
return P_mobile;
}
/**
* 字符串转set
*
* @param value
* 字符串
* @return
*/
public static Set<String> strToSet(String value) {
return strToSet(value, ",");
}
/**
* 字符串转list
*
* @param value
* 字符串
* @return
*/
public static List<String> strToList(String value) {
return strToList(value, ",");
}
/**
* 字符串转set
*
* @param value
* 字符串
* @param split
* 分割符
* @return
*/
public static Set<String> strToSet(String value, String split) {
Set<String> V_toIds = new HashSet<>();
String[] R_sysIdsAry = JavaUtil.split(value, split);
for (String VF_value : R_sysIdsAry) {
V_toIds.add(VF_value);
}
return V_toIds;
}
/**
* 字符串转list
*
* @param value
* 字符串
* @param split
* 分割符
* @return
*/
public static List<String> strToList(String value, String split) {
List<String> V_toIds = new ArrayList<>();
String[] R_sysIdsAry = JavaUtil.split(value, split);
for (String VF_value : R_sysIdsAry) {
V_toIds.add(VF_value);
}
return V_toIds;
}
}/**
* @Description TestJavaUtil.java
* @author 张军
* @date 2022年3月6日 上午10:48:53
* @version V1.0
*/
package test.java;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import zj.check.util.CheckUtil;
import zj.java.util.JavaUtil;
/**
* @author 张军
* @date 2022年3月6日 上午10:48:53
* @version V1.0
*/
public class TestJavaUtil {
@Test
public void 分割email地址转换成一行() {
String 前台传的email值 = "";
for (int a = 1; a <= 5; a++) {
if (CheckUtil.isNotNull(前台传的email值)) {
前台传的email值 += "\r\n";
}
前台传的email值 += "zhangjun" + a + "@126.com";
}
System.out.println("前台传的email值:\n" + 前台传的email值);
String 分割后结果 = JavaUtil.emailSplitToOneline(前台传的email值);
System.out.println("分割后结果:\n" + 分割后结果);
}
@Test
public void 判断系统() {
System.out.println("是否是windows:" + JavaUtil.isWindows());
System.out.println("是否是mac系统:" + JavaUtil.isMac());
}
@Test
public void 中文数字转数字() {
System.out.println("中文数字转数字:" + JavaUtil.getIntegerByStr("六万七千三百八十二"));
System.out.println("中文数字转数字:" + JavaUtil.getIntegerByStr("五千零一"));
}
@Test
public void 数字转大写() {
System.out.println("数字转大写:" + JavaUtil.numToCnChar("1234567890"));
}
@Test
public void 数字转大写日期() {
System.out.println("4位数字年:" + JavaUtil.numToCnCharDate(2022));
System.out.println("2位数字月或日:" + JavaUtil.numToCnCharDate(12));
System.out.println("1位数字月或日:" + JavaUtil.numToCnCharDate(9));
}
@Test
public void 数值转换为中文字符串() {
System.out.println("数值转换为中文字符串:" + JavaUtil.numToCn(20221231));
System.out.println("第2个参数是否口语化。例如12转换为'十二'而不是'一十二'。:" + JavaUtil.numToCnCharDate(12));
}
@Test
public void 获取集合差异() {
List<String> l1 = new ArrayList<>();
l1.add("a");
l1.add("b");
l1.add("c");
l1.add("d");
l1.add("e");
List<String> l2 = new ArrayList<>();
l2.add("a1");
l2.add("b");
l2.add("c1");
l2.add("d");
l2.add("e1");
System.out.println("获取集合差异:" + JavaUtil.diffColl(l1, l2));
}
@Test
public void 数字格式化() {
System.out.println("数字格式化:" + JavaUtil.covertToNumSplit(1234567890));
}
@Test
public void 转为大数字() {
System.out.println("转为大数字:" + JavaUtil.covertBigDecimal(1234567890));
System.out.println("转为大数字:" + JavaUtil.covertBigDecimal("1234567890123456789").setScale(5, BigDecimal.ROUND_HALF_UP));
}
@Test
public void 获取in的字符串() {
List<String> l2 = new ArrayList<>();
l2.add("a1");
l2.add("b");
l2.add("c1");
l2.add("d");
l2.add("e1");
System.out.println("获取in的字符串: in(" + JavaUtil.sqlInStr(l2) + ")");
}
@Test
public void 转换文件大小() {
System.out.println("转换文件大小为B、KB、MB、GB:" + JavaUtil.convertFileSize(123456));
System.out.println("转换文件大小为B、KB、MB、GB:" + JavaUtil.convertFileSize(1234567890));
}
@Test
public void 深度拷贝list数据() {
List<String> 源集合 = new ArrayList<>();
源集合.add("a");
源集合.add("b");
源集合.add("c");
源集合.add("d");
源集合.add("e");
List<String> 目标集合 = JavaUtil.deepCopy(源集合);
源集合.remove(0);
源集合.remove(1);
System.out.println("源集合:" + 源集合);
System.out.println("移除src里的元素,dest元素不会发生变化");
System.out.println("目标集合:" + 目标集合);
}
@Test
public void 合并多个数组() {
String[] 数组1 = new String[] { "a", "b", "c" };
String[] 数组2 = new String[] { "d", "e", "f" };
String[] 数组3 = new String[] { "11", "22", "33" };
System.out.println("数组1:" + Arrays.toString(数组1));
System.out.println("数组2:" + Arrays.toString(数组2));
System.out.println("数组3...n:" + Arrays.toString(数组3));
System.out.println("合并N个数组:" + Arrays.toString(JavaUtil.concatArrayAll(数组1, 数组2, 数组3)));
}
@Test
public void 按实际字符分割字符串() {
String 字符串 = "a.b.c.d.e";
System.out.println("按实际字符分割字符串:" + Arrays.toString(JavaUtil.split(字符串, ".")));
}
@Test
public void LIST转数组() {
List<String> 源集合 = new ArrayList<>();
源集合.add("a");
源集合.add("b");
源集合.add("c");
源集合.add("d");
源集合.add("e");
System.out.println("LIST转数组:" + Arrays.toString(JavaUtil.listToArray(源集合, String.class)));
}
@Test
public void 获取最大长度文字() {
System.out.println("获取最大长度文字:" + JavaUtil.getMaxText("获取最大长度文字", 3));
}
@Test
public void 去除数组中为null或空的值() {
String[] 数组1 = new String[] { "a", "b", "c", "", "", null, "\n", "666666666" };
System.out.println("去除数组前的值:" + Arrays.toString(数组1));
System.out.println("去除数组中为null或空的值:" + Arrays.toString(JavaUtil.aryTrimValue(数组1)));
}
@Test
public void 提取字符串中的整数字() {
System.out.println("提取字符串:" + "我是123中456国7890结束");
System.out.println("提取字符串中的整数字:" + JavaUtil.conversionStringToNum("我是123中456国7890结束"));
}
@Test
public void 提取字符串中的整数字带小数或正负数() {
System.out.println("提取字符串:" + "我是-123中456国7.890结束");
System.out.println("提取字符串中的整数字带小数或正负数:" + JavaUtil.conversionStringToNumber("我是-123中456国7.890结束"));
}
@Test
public void 固定集合元素() {
int count = 10;
List<String> 原集合值 = new ArrayList<String>();
for (int i = 0; i < count; i++) {
原集合值.add("值:" + i);
}
System.out.println("原集合值:" + 原集合值);
List<List<String>> 固定批量值 = JavaUtil.getBatchList(原集合值, 3);
System.out.println("固定集合元素,每个集合3个元素:" + 固定批量值);
}
@Test
public void 固定批量集合() {
int count = 10;
List<String> 原集合值 = new ArrayList<String>();
for (int i = 0; i < count; i++) {
原集合值.add("值:" + i);
}
System.out.println("原集合值:" + 原集合值);
List<List<String>> 固定批量值 = JavaUtil.getFixedBatchList(原集合值, 3);
System.out.println("固定批量集合,3个批次元素:" + 固定批量值);
}
@Test
public void 根据页码条数计算多少页() {
System.out.println("总条数:" + 10);
System.out.println("每页显示条数:" + 3);
System.out.println("根据页码条数计算多少页:" + JavaUtil.calcPageCount(10, 3));
}
@Test
public void 获取最后分割符前面的字符串() {
String 字符串 = "获取最后分割符#前面的字符串";
System.out.println("字符串:" + 字符串);
System.out.println("获取最后分割符#前面的字符串:" + JavaUtil.getBeforeTextLastDelimiter(字符串, "#"));
}
@Test
public void 获取最后分割符后面的字符串() {
String 字符串 = "获取最后分割符#前面的字符串";
System.out.println("字符串:" + 字符串);
System.out.println("获取最后分割符#前面的字符串:" + JavaUtil.getAfterTextLastDelimiter(字符串, "#"));
}
@Test
public void 去除小数点后的字符() {
System.out.println("去除前字符:123.12345000");
System.out.println("去除小数点后为0的字符:" + JavaUtil.getIsNumValue(123.12345000));
System.out.println("去除前字符:12345678.00000");
System.out.println("去除小数点后为0的字符:" + JavaUtil.getIsNumValue("12345678.00000"));
}
@Test
public void 获取map中的值为map对象() {
Map<String, Map<String, Object>> map = new HashMap<>();
Map<String, Object> value = new HashMap<>();
value.put("test", "我是值a");
map.put("a", value);
value = new HashMap<>();
value.put("test", "我是值b");
map.put("b", value);
value = new HashMap<>();
value.put("test", "我是值c");
map.put("c", value);
System.out.println("map:" + map);
System.out.println("获取map中key为b的值为map对象:" + JavaUtil.getValueForMap(map, "b"));
System.out.println("获取map中key为d的值为map对象:" + JavaUtil.getValueForMap(map, "d"));
}
@Test
public void 获取map中的值为list_map对象() {
Map<String, List<Map<String, Object>>> map = new HashMap<>();
List<Map<String, Object>> values = new ArrayList<>();
Map<String, Object> value = new HashMap<>();
values.add(value);
value.put("test", "我是值a");
map.put("a", values);
value = new HashMap<>();
values.add(value);
value.put("test", "我是值b");
map.put("b", values);
value = new HashMap<>();
values.add(value);
value.put("test", "我是值c");
map.put("c", values);
System.out.println("map:" + map);
System.out.println("获取map中的值为list.map对象:" + JavaUtil.getValueForListMap(map, "b"));
System.out.println("获取map中的值为list.map对象:" + JavaUtil.getValueForListMap(map, "d"));
}
}map:{b=[{test=我是值a}, {test=我是值b}, {test=我是值c}], c=[{test=我是值a}, {test=我是值b}, {test=我是值c}], a=[{test=我是值a}, {test=我是值b}, {test=我是值c}]}
获取map中的值为list.map对象:[{test=我是值a}, {test=我是值b}, {test=我是值c}]
获取map中的值为list.map对象:[]
map:{b={test=我是值b}, c={test=我是值c}, a={test=我是值a}}
获取map中key为b的值为map对象:{test=我是值b}
获取map中key为d的值为map对象:{}
原集合值:[值:0, 值:1, 值:2, 值:3, 值:4, 值:5, 值:6, 值:7, 值:8, 值:9]
固定批量集合,3个批次元素:[[值:0, 值:1, 值:2, 值:3], [值:4, 值:5, 值:6, 值:7], [值:8, 值:9]]
获取集合差异:[e1, e, c1, c, a, a1]
原集合值:[值:0, 值:1, 值:2, 值:3, 值:4, 值:5, 值:6, 值:7, 值:8, 值:9]
固定集合元素,每个集合3个元素:[[值:0, 值:1, 值:2], [值:3, 值:4, 值:5], [值:6, 值:7, 值:8], [值:9]]
数字格式化:1,234,567,890
数字转大写:一二三四五六七八九零
数组1:[a, b, c]
数组2:[d, e, f]
数组3...n:[11, 22, 33]
合并N个数组:[a, b, c, d, e, f, 11, 22, 33]
提取字符串:我是123中456国7890结束
提取字符串中的整数字:1234567890
数值转换为中文字符串:二千零二十二万一千二百三十一
第2个参数是否口语化。例如12转换为'十二'而不是'一十二'。:十二
4位数字年:二零二二
2位数字月或日:十二
1位数字月或日:九
提取字符串:我是-123中456国7.890结束
提取字符串中的整数字带小数或正负数:-1234567.890
转为大数字:1234567890
转为大数字:1234567890123456789.00000
是否是windows:true
是否是mac系统:false
LIST转数组:[a, b, c, d, e]
前台传的email值:
zhangjun1@126.com
zhangjun2@126.com
zhangjun3@126.com
zhangjun4@126.com
zhangjun5@126.com
分割后结果:
zhangjun1@126.com;zhangjun2@126.com;zhangjun3@126.com;zhangjun4@126.com;zhangjun5@126.com
字符串:获取最后分割符#前面的字符串
获取最后分割符#前面的字符串:前面的字符串
字符串:获取最后分割符#前面的字符串
获取最后分割符#前面的字符串:获取最后分割符
获取in的字符串: in('a1','b','c1','d','e1')
获取最大长度文字:获取最...
去除前字符:123.12345000
去除小数点后为0的字符:123.12345
去除前字符:12345678.00000
去除小数点后为0的字符:12345678
去除数组前的值:[a, b, c, , , null,
, 666666666]
去除数组中为null或空的值:[a, b, c, 666666666]
按实际字符分割字符串:[a, b, c, d, e]
转换文件大小为B、KB、MB、GB:120.56KB
转换文件大小为B、KB、MB、GB:1.15GB
中文数字转数字:67382
中文数字转数字:5001
总条数:10
每页显示条数:3
根据页码条数计算多少页:4
源集合:[b, d, e]
移除src里的元素,dest元素不会发生变化
目标集合:[a, b, c, d, e]
本文为张军原创文章,转载无需和我联系,但请注明来自张军的军军小站,个人博客http://www.zhangjunbk.com

