个人总结的Java工具类
有补充的欢迎大家帮我补充
- list去除中括号
- 时间转换(过去了多长时间)
- 判断参数是不是为空或者为null
- 模糊搜索(加强)
- 正则判断是否包含大小写
- 判断手机号格式是否正确
- 获取IP地址
- 获取mac地址
- 获取uuid 32位 64位
- 判断是否包含
- 数组分页
- 数组去重
- 身份证获取性别
- 生日判断年龄
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 个人工具类
*
* @author zhujx
* @date 2019-09-18 17:07
**/
@Slf4j
public class PersonalUtils {
/**
* list去除中括号
*
* @param list 数组
* @return java.lang.String
* @author zhujx
* @date 2019/7/3
*/
public static String idsToString(List<Long> list) {
return StringUtils.join(list, ",");
}
/**
* 时间转换
*
* @param time 时间戳
* @return java.lang.String
* @author zhujx
* @date 2019/8/6
*/
public static String stampToDate(Long time) {
time = time / 1000;
String dateTimes;
long days = time / (60 * 60 * 24);
long hours = (time % (60 * 60 * 24)) / (60 * 60);
long minutes = (time % (60 * 60)) / 60;
long seconds = time % 60;
if (days > 0) {
dateTimes = days + "天" + hours + "小时" + minutes + "分钟";
} else if (hours > 0) {
dateTimes = hours + "小时" + minutes + "分钟";
} else if (minutes > 0) {
dateTimes = minutes + "分钟";
} else {
dateTimes = seconds + "秒";
}
return dateTimes;
}
/**
* 判断参数
*
* @param obj 参数
* @return boolean
* @author zhujx
* @date 2019/8/14
*/
public static boolean isBlank(Object... obj) {
boolean isTrue = false;
for (Object o : obj) {
if (o == null) {
return true;
} else if (o instanceof String) {
isTrue = isTrue || StringUtils.isBlank(o.toString());
} else if (o instanceof List) {
isTrue = isTrue || ((List) o).isEmpty();
}
}
return isTrue;
}
/**
* 模糊搜索sql
*
* @param name 参数
* @return java.lang.String
* @author zhujx
* @date 2019/8/23
*/
public static String getLikeName(String name) {
if (isBlank(name)) {
return null;
} else {
return "%" + name + "%";
}
}
/**
* 加强搜索sql
*
* @param name 参数
* @return java.lang.String
* @author zhujx
* @date 2019/8/23
*/
public static String getLikeNameUp(String name) {
if (isBlank(name)) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
sb.append("%");
sb.append(name.charAt(i));
}
return sb.append("%").toString();
}
private static final Pattern PATTERN_UP = Pattern.compile("^.*[A-Z]+.*$");
private static final Pattern PATTERN_LOW = Pattern.compile("^.*[a-z]+.*$");
/**
* 正则表达式判断是否包括大小写
*
* @param password 密码
* @return boolean
* @author zhujx
* @date 2019/8/23
*/
public static boolean pattern(String password) {
Matcher matcher = PATTERN_UP.matcher(password);
Matcher matcher1 = PATTERN_LOW.matcher(password);
return matcher.matches() && matcher1.matches();
}
private static final Pattern MOBILE = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$");
/**
* 判断手机号是否正确
*
* @param phone 手机号
* @return boolean
* @author zhujx
* @date 2019/9/18
*/
public static boolean isPhone(String phone) throws Exception {
int mobileSize = 11;
if (phone.length() != mobileSize) {
throw new Exception("手机号应为11位数!");
} else {
Matcher m = MOBILE.matcher(phone);
boolean isMatch = m.matches();
if (!isMatch) {
throw new Exception("请填入正确的手机号!");
} else {
return true;
}
}
}
/**
* 获取ip
*
* @param request request
* @return java.lang.String
* @author zhujx
* @date 2020/12/14 13:47
*/
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip.contains(",")) {
return ip.split(",")[0];
} else {
return ip;
}
}
/**
* 获得32长度的UUID字符串
*
* @return java.lang.String
* @author zhujx
* @date 2020/4/20
*/
public static String getUuid32() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 获得64长度的UUID字符串
*
* @return java.lang.String
* @author zhujx
* @date 2020/4/20
*/
public static String getUuid64() {
return getUuid32() + getUuid32();
}
/**
* 获取本机的mac地址 windows版
*
* @return java.lang.String
* @author zhujx
* @date 2021/3/5 10:28
*/
public static String getLocalMac() throws Exception {
InetAddress ia = InetAddress.getLocalHost();
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
sb.append("0").append(str);
} else {
sb.append(str);
}
}
return sb.toString().toUpperCase();
}
/**
* 判断包含
*
* @param value 值
* @param values 值数组
* @return boolean
* @author zhujx
* @date 2019/12/18
*/
public static boolean isEquals(Object value, Object... values) {
boolean isTrue = false;
if (value != null) {
for (Object obj : values) {
if (value instanceof Integer || value instanceof Long || value instanceof String) {
isTrue = isTrue || value.equals(obj);
}
}
}
return isTrue;
}
/**
* 分页
*
* @param list list
* @param pageNum 当前页
* @param pageSize 每页条数
* @return java.util.List<T>
* @author zhujx
* @date 2020/1/8
*/
public static <T> List<T> getPage(List<T> list, int pageNum, int pageSize) {
int total = list.size();
int start = (pageNum - 1) * pageSize;
int end = pageNum * pageSize;
List<T> newList = new ArrayList<>();
for (int i = start; i < (Math.min(end, total)); i++) {
newList.add(list.get(i));
}
return newList;
}
/**
* 去重
*
* @param oldList 原数组
* @param newList 新数组
* @return java.util.List<T>
* @author zhujx
* @date 2020/2/15
*/
public static <T> List<T> removeList(List<T> oldList, List<T> newList) {
List<T> list = new ArrayList<>();
if (oldList.isEmpty() && newList.isEmpty()) {
return list;
} else if (oldList.isEmpty()) {
return newList;
} else if (newList.isEmpty()) {
return list;
} else {
for (T newT : newList) {
boolean isTrue = true;
for (T t : oldList) {
if (t.equals(newT)) {
isTrue = false;
break;
}
}
if (isTrue) {
list.add(newT);
}
}
}
return list;
}
/**
* 18位身份证获取性别
*
* @param idCard 身份证号码
* @return java.lang.String
* @author zhujx
* @date 2020/7/10 10:21
*/
public static String getSexByIdCard(String idCard) {
if (StringUtils.isBlank(idCard)) {
return null;
}
if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {
return "女";
} else {
return "男";
}
}
/**
* 生日获取年龄
*
* @param birth 年月日
* @param pattern 格式 yyyy-MM-dd...
* @return java.lang.Integer
* @author zhujx
* @date 2020/7/10 10:21
*/
public static Integer getAgeByBirth(String birth, String pattern) {
if (StringUtils.isBlank(birth) || StringUtils.isBlank(pattern)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
try {
Date birthDate = sdf.parse(birth);
cal.setTime(birthDate);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
}
} else {
age--;
}
}
return age;
} catch (ParseException e) {
log.error("getAgeByBirth date error birth = {} ,pattern = {}", birth, pattern);
return null;
}
}
}