工具笔记 —— 常用自定义工具类(正则,随机数等)

匹配邮箱

/**
 * 邮箱校验
 *
 * @param email 邮箱
 * @return true or false
 */
public static boolean isEmail(String email) {
    
    
    Matcher m = Pattern.compile("^([a-zA-Z]|[0-9])(\\w|\\-)+@[a-zA-Z0-9]+\\.([a-zA-Z]{2,4})$").matcher(email);
    return m.matches();
}

匹配国内11位手机号码

/**
 * 手机号码校验
 *
 * @param phone
 * @return true or false
 */
public static boolean isPhone(String phone) {
    
    
    Matcher m = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$").matcher(phone);
    return m.matches();
}

匹配5-20个只能包含数字和字母,且至少包含1个字母

/**
 * 长度 5-20 个字符,只能包含数字、大小写字母 且 至少包含一个字母
 *
 * @param str 待校验字符串
 * @return true or false
 */
public static boolean isContainOneLetter(String str) {
    
    
    Matcher m = Pattern.compile("(?=.*[a-zA-Z])[a-zA-Z0-9]{5,20}").matcher(str);
    return m.matches();
}

判断是否有中文(不包括中文符号)

/**
 * 检验字符串中是否包含中文,不包括中文字符
 *
 * @param str 待验证字符串
 * @return true-包含  false-不包含
 */
public static boolean isContainsChinese(String str) {
    
    
    Matcher m = Pattern.compile("[\u4e00-\u9fa5]").matcher(str);
    return m.find();
}

判断某个字符是不是表情

/**
 * 判断某个字符是不是表情
 *
 * @param codePoint
 * @return true or false
 */
private static boolean isEmojiCharacter(char codePoint) {
    
    
    return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
            || (codePoint == 0xD)
            || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
            || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
            || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}

判断字符串中是否包含表情

/**
 * 判断字符串中是否含有表情
 *
 * @param source
 * @return true or false
 */
public static boolean isContainsEmoji(String source) {
    
    
    int len = source.length();
    boolean isEmoji = false;
    for (int i = 0; i < len; i++) {
    
    
        char hs = source.charAt(i);
        if (0xd800 <= hs && hs <= 0xdbff) {
    
    
            if (source.length() > 1) {
    
    
                char ls = source.charAt(i + 1);
                int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
    
    
                    return true;
                }
            }
        } else {
    
    
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
    
    
                return true;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
    
    
                return true;
            } else if (0x2934 <= hs && hs <= 0x2935) {
    
    
                return true;
            } else if (0x3297 <= hs && hs <= 0x3299) {
    
    
                return true;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
                    || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
                    || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
    
    
                return true;
            }
            if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
    
    
                char ls = source.charAt(i + 1);
                if (ls == 0x20e3) {
    
    
                    return true;
                }
            }
        }
    }
    return isEmoji;
}

过滤掉字符串中的表情

/**
 * 过滤掉字符串中的表情
 *
 * @param source
 * @return 过滤后的字符串
 */
public static String filterEmoji(String source) {
    
    
    if (StringUtils.isBlank(source)) {
    
    
        return source;
    }
    StringBuilder buf = null;
    int len = source.length();
    for (int i = 0; i < len; i++) {
    
    
        char codePoint = source.charAt(i);
        if (isEmojiCharacter(codePoint)) {
    
    
            if (buf == null) {
    
    
                buf = new StringBuilder(source.length());
            }
            buf.append(codePoint);
        }
    }
    if (buf == null) {
    
    
        return source;
    } else {
    
    
        if (buf.length() == len) {
    
    
            buf = null;
            return source;
        } else {
    
    
            return buf.toString();
        }
    }
}

随机生成6位数字验证码

/**
 * 随机生成六位数字验证码
 */
public static String randomSixCode() {
    
    
    return String.valueOf(new Random().nextInt(899999) + 100000);
}

数值转换为万单位(如文章访问量等)

public static String viewNumberFormat(Integer number) {
    
    
	if (number < 10000) {
    
    
		return number.toString();
	}else if (number <= 100000) {
    
    
        return number / 1000 % 10 == 0 ? number / 10000 + "万+" : number / 10000 + "." + number / 1000 % 10 + "万+";
	}else {
    
    
        return number / 10000 + "万+";
    }
}

日期格式转换(字符串 -> Date)

年-月-日 格式

/**
 * 由 Date格式日期 转换为 String格式日期
 * @param date 原日期
 * @param isWhole 是否全路径(true -> 年-月-日 格式,false -> 年-月-日 时:分:秒 格式)
 * @return 转换后的日期
 */
public static String toStringFromDate(Date date, boolean isWhole) {
    
    
    SimpleDateFormat format;
    if (isWhole) {
    
    
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }else {
    
    
        format = new SimpleDateFormat("yyyy-MM-dd");
    }
    return format.format(date);
}

日期格式转换(Date -> 字符串)

/**
 * 由 String格式日期 转换为 Date格式日期
 * @param date 原日期
 * @param isWhole 是否全路径(true -> 年-月-日 格式,false -> 年-月-日 时:分:秒 格式)
 * @return 转换后的日期
 */
public static Date toDateFromString(String date, boolean isWhole) {
    
    
    SimpleDateFormat format;
    if (isWhole) {
    
    
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }else {
    
    
        format = new SimpleDateFormat("yyyy-MM-dd");
    }
    Date resultDate = new Date();
    try {
    
    
        resultDate =  format.parse(date);
    } catch (ParseException e) {
    
    
        e.printStackTrace();
    }
    return resultDate;
}

Long类型主键简单处理(异或加密)

加密

/**
 *
 * @param key 待加密字符串 (如 1524415450874658817)
 * @return 加密后的字符串(如 21d94c70ac5ae008746588179)
 */
public static String encrypt(String key) {
    
    
    // 对 10 - length 长度的数字进行转换
    Integer src = Integer.parseInt(key.substring(0, 10));

    String timeStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));

    long pwd = Long.parseLong(src + timeStr);
    long xorPwd = pwd ^ Integer.parseInt(timeStr);

    // 回传转换后的字符串
    return Long.toHexString(xorPwd) + key.substring(10) + (key.length() - 10);
}

解密

/**
 * 
 * @param key 待解密字符串 (如 21d94c70ac5ae008746588179)
 * @return 解密后的字符串(如 1524415450874658817)
 */
public static String decode(String key) {
    
    
    try {
    
    
        int start = key.length() - Integer.parseInt(key.substring(key.length() - 1));
        // 截取 10 - length 长度进行解码
        key = key.substring(0, key.length() - 1);
        String src = key.substring(0, start - 1);

        Long timeInt = Long.parseLong(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));

        Long xorOrigin = (Long.parseLong(src, 16)) ^ timeInt;
        Long id = (xorOrigin - timeInt) / 100000000;

        // 回传解码的字符串
        if (id * 100000000 == xorOrigin - timeInt) {
    
    
            return id.intValue() + key.substring(start - 1);
        }
    }catch (StringIndexOutOfBoundsException | NumberFormatException e) {
    
    
        return "";
    }
    return "";
}

猜你喜欢

转载自blog.csdn.net/weixin_47971206/article/details/125236283