String类正则表达式

public static String convertTimeToString(Long time) {
    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}


//    字符串转UTF-8格式
public static String decode(String str) {
    if (str != null) {
        byte[] b = new byte[0];
        try {
            b = str.getBytes();
            String newString = new String(b, "UTF-8");
            return newString;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return "";
}

/**
 * 切换大小端续
 *
 * @param a 要转还的数组
 * @return 转换后的数组
 */
public static byte[] changeBytes(byte[] a) {
    byte[] b = new byte[a.length];
    for (int i = 0; i < b.length; i++) {
        b[i] = a[b.length - i - 1];
    }
    return b;
}

/**
 * 判断IP地址的合法性,这里采用了正则表达式的方法来判断
 * return true,合法
 */
public static boolean ipCheck(String text) {
    if (text != null && !text.isEmpty()) { // 定义正则表达式
        String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." +
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";

        // 判断ip地址是否与正则表达式匹配
        if (text.matches(regex)) { // 返回判断信息
            return true;
        } else { // 返回判断信息
            return false;
        }
    }
    return false;
}

以上内容供参考,以后会根据业务增加

猜你喜欢

转载自blog.csdn.net/langxian_168/article/details/86612085
今日推荐