个人总结的Java小工具类

个人总结的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;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值