com.ck.tools.Utils Maven / Gradle / Ivy
package com.ck.tools;
import com.github.pagehelper.PageHelper;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.*;
public class Utils {
public static Random random = new Random();
/***
* 判断对象是否为 空|null|''|' '|""
* @param obj Object
* @return 是:true 否:false
*/
public static boolean isNull(Object obj) {
if (null == obj)
return true;
if (obj instanceof List) {
List> list = (List>) obj;
return list.isEmpty() ? true : false;
}
if (obj instanceof Map) {
Map, ?> map = (Map, ?>) obj;
return map.isEmpty() ? true : false;
}
if (obj instanceof Set) {
Set> set = (Set>) obj;
return set.isEmpty() ? true : false;
}
if (obj instanceof String) {
String str = (String) obj;
if (str.trim().equals("\"\"") || str.trim().equals("\'\'")) {
return true;
}
return str.trim().isEmpty() ? true : false;
}
if (obj instanceof Character) {
return ((Character) obj).charValue() == ' ' ? true : false;
}
return false;
}
/***
* 生成一个八位数的随机数
* @return String
*/
public static String unionHexStr() {
return Integer.toHexString((int) System.nanoTime() + (int) (Math.random() * 9 + 1) * 1000);
}
/***
* 随机生成一串指定位数的数字
* @param length 位数
* @return String
*/
public static String getVerityCode(int length) {
String val = "";
for (int i = 0; i < length; i++) {
val += String.valueOf(random.nextInt(10));
}
return val.toLowerCase();
}
/***
* mybatis 分页工具
* @param page int
* @param limit int
*/
public static void offsetPage(int page, int limit) {
if (page < 1) throw new IllegalArgumentException("page number must not be less than 1");
PageHelper.clearPage();
if (PageHelper.getLocalPage() != null) {
PageHelper.getLocalPage().setOrderByOnly(false);
}
PageHelper.offsetPage((page - 1) * limit, limit);
}
/***
* 字符串转集合
* @param target String
* @param split String
* @return List
*/
public static List strToStringList(String target, String... split) {
List list = new ArrayList();
if (Utils.isNull(target)) return list;
String splitStr = ",";
if (split.length > 0) {
splitStr = split[0];
}
for (String s : target.split(splitStr)) {
if(!Utils.isNull(s))
list.add(s);
}
return list;
}
/***
* unicode 转换成 中文
* @param theString String
* @return string
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't') {
aChar = '\t';
} else if (aChar == 'r') {
aChar = '\r';
} else if (aChar == 'n') {
aChar = '\n';
} else if (aChar == 'f') {
aChar = '\f';
}
outBuffer.append(aChar);
}
} else {
outBuffer.append(aChar);
}
}
return outBuffer.toString();
}
/***
* 脱敏数据
* @param data 明文
* @param prefix 明文显示的前几位数据的个数
* @param suffix 明文显示的后几位数据的个数
* @param hide 脱敏中间数据的个数
* @return 152****5455 例:脱敏手机号 hiddenData("15265985455",3,4,4);
*/
public static String hiddenData(String data,int prefix,int suffix,int hide){
String str = "";
for (int i = 0; i < hide; i++){ str += "*";}
return data.replaceAll("(\\S{"+prefix+"})\\S{"+hide+"}(\\S{"+suffix+"})","$1"+str+"$2");
}
/***
* 脱敏数据
* @param data 明文
* @param prefix 明文显示的前几位数据的个数
* @param suffix 明文显示的后几位数据的个数
* @param hide 脱敏中间数据的个数
* @param symbol 脱敏显示的符号
* @return 152****5455 例:脱敏手机号 hiddenData("15265985455",3,4,4);
*/
public static String hiddenData(String data,int prefix,int suffix,int hide, String symbol){
if(Utils.isNull(symbol)) symbol = "*";
String str = "";
for (int i = 0; i < hide; i++){ str += symbol;}
return data.replaceAll("(\\S{"+prefix+"})\\S{"+hide+"}(\\S{"+suffix+"})","$1"+str+"$2");
}
/***
* md5加密
* @param str String
* @return string
*/
public static String md5(String str) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] bytes = str.getBytes();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(bytes);
byte[] updateBytes = messageDigest.digest();
int len = updateBytes.length;
char myChar[] = new char[len * 2];
int k = 0;
for (int i = 0; i < len; i++) {
byte byte0 = updateBytes[i];
myChar[k++] = hexDigits[byte0 >>> 4 & 0x0f];
myChar[k++] = hexDigits[byte0 & 0x0f];
}
return new String(myChar);
} catch (Exception e) {
return null;
}
}
public static String join(Object[] array, String sep) {
return join((Object[])array, sep, (String)null);
}
public static String join(Object[] array, String sep, String prefix) {
if (array == null) {
return "";
} else {
int arraySize = array.length;
if (arraySize == 0) {
return "";
} else {
if (sep == null) {
sep = "";
}
if (prefix == null) {
prefix = "";
}
StringBuilder buf = new StringBuilder(prefix);
for(int i = 0; i < arraySize; ++i) {
if (i > 0) {
buf.append(sep);
}
buf.append(array[i] == null ? "" : array[i]);
}
return buf.toString();
}
}
}
/***
* 除法
* @param arg1 String
* @param arg2 String
* @return BigDecimal
*/
public static BigDecimal divide(String arg1, String arg2) {
if (Utils.isNull(arg1)) {
arg1 = "0.0";
}
if (Utils.isNull(arg2)) {
arg2 = "0.0";
}
BigDecimal big3 = new BigDecimal("0.00");
if (Double.parseDouble(arg2) != 0) {
BigDecimal big1 = new BigDecimal(arg1);
BigDecimal big2 = new BigDecimal(arg2);
big3 = big1.divide(big2, 2, BigDecimal.ROUND_HALF_EVEN);
}
return big3;
}
/***
* 乘法
* @param arg1 String
* @param arg2 String
* @return BigDecimal
*/
public static BigDecimal mul(String arg1, String arg2) {
if (Utils.isNull(arg1)) {
arg1 = "0.0";
}
if (Utils.isNull(arg2)) {
arg2 = "0.0";
}
BigDecimal big1 = new BigDecimal(arg1);
BigDecimal big2 = new BigDecimal(arg2);
BigDecimal big3 = big1.multiply(big2);
return big3;
}
/***
* 减法
* @param arg1 String
* @param arg2 String
* @return BigDecimal
*/
public static BigDecimal sub(String arg1, String arg2) {
if (Utils.isNull(arg1)) {
arg1 = "0.0";
}
if (Utils.isNull(arg2)) {
arg2 = "0.0";
}
BigDecimal big1 = new BigDecimal(arg1);
BigDecimal big2 = new BigDecimal(arg2);
BigDecimal big3 = big1.subtract(big2);
return big3;
}
/***
* 加法
* @param arg1 String
* @param arg2 String
* @return BigDecimal
*/
public static BigDecimal add(String arg1, String arg2) {
if (Utils.isNull(arg1)) {
arg1 = "0.0";
}
if (Utils.isNull(arg2)) {
arg2 = "0.0";
}
BigDecimal big1 = new BigDecimal(arg1);
BigDecimal big2 = new BigDecimal(arg2);
BigDecimal big3 = big1.add(big2);
return big3;
}
/***
* 加法
* @param arg1 String
* @param arg2 String
* @return String
*/
public static String add2(String arg1, String arg2) {
if (Utils.isNull(arg1)) {
arg1 = "0.0";
}
if (Utils.isNull(arg2)) {
arg2 = "0.0";
}
BigDecimal big1 = new BigDecimal(arg1);
BigDecimal big2 = new BigDecimal(arg2);
BigDecimal big3 = big1.add(big2);
return big3.toString();
}
/**
* 四舍五入保留N位小数 先四舍五入在使用double值自动去零
*
* @param arg 参数
* @param scare 保留位数
* @return string
*/
public static String setScare(BigDecimal arg, int scare) {
BigDecimal bl = arg.setScale(scare, BigDecimal.ROUND_HALF_UP);
return String.valueOf(bl.doubleValue());
}
public static double setDifScare(double arg) {
BigDecimal bd = new BigDecimal(arg);
BigDecimal bl = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return Double.parseDouble(bl.toString());
}
/**
* 四舍五入保留两位小数 先四舍五入在使用double值自动去零
*
* @param arg 参数
* @return string
*/
public static String setDifScare(String arg) {
BigDecimal bd = new BigDecimal(arg);
BigDecimal bl = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return bl.toString();
}
/***
* 四舍五入保留N位小数 先四舍五入在使用double值自动去零(传参String类型)
* @param arg 参数
* @param i int
* @return string
*/
public static String setDifScare(String arg, int i) {
BigDecimal bd = new BigDecimal(arg);
BigDecimal bl = bd.setScale(i, BigDecimal.ROUND_HALF_UP);
return bl.toString();
}
/**
* 转化成百分数 先四舍五入在使用double值自动去零
*
* @param arg 参数
* @return string
*/
public static String setFenScare(BigDecimal arg) {
BigDecimal bl = arg.setScale(3, BigDecimal.ROUND_HALF_UP);
String scare = String.valueOf(mul(bl.toString(), "100").doubleValue());
String fenScare = scare + "%";
return fenScare;
}
/**
* 使用java正则表达式去掉多余的.与0
*
* @param s 参数
* @return string
*/
public static String subZeroAndDot(String s) {
if (s.indexOf(".") > 0) {
s = s.replaceAll("0+?$", "");// 去掉多余的0
s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
}
return s;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy