java 后续补充

358 阅读3分钟

JDK 自带

MessageDigest md5 加密

// 创建md5加密对象
MessageDigest md5 = MessageDigest.getInstance("md5");
Base64.Encoder encoder = Base64.getEncoder();
//转换成字符串
new String(encoder.encode(md5.digest(passwd.getBytes())), StandardCharsets.ISO_8859_1);

Base64 加密

@Test
void practise() {
    // 创建对象
    Base64.Encoder encoder = Base64.getEncoder();
    // 把20加密
    String base64UserIDEncoded = encoder.encodeToString(String.valueOf(20).getBytes());
    // 把当前时间加密
    String currentStringBase64Encoded = encoder.encodeToString(String.valueOf(System.currentTimeMillis()).getBytes());
    // 进行拼接
    String s = currentStringBase64Encoded + currentStringBase64Encoded.substring(4, 8) + base64UserIDEncoded;
    // 拼接好转数组
    byte[] bytes = s.getBytes();
    // 将该字符串的长度转成数组
    byte[] bytes1 = new byte[s.length()];
    // 元素进行加减,数据更换 ,这里直接反转也可以,这里使用的是计算
    for (int i = 0; i < bytes.length; i++) {
        bytes1[i] = bytes[bytes.length - i - 1];
    }
    // 把=号用#号替换
    String s1 = new String(bytes1).replaceAll("=","#");
}

Base64 解密

@Test
void parent() {
    // 解密前先判断会不会是空的
    if(StringUtils.isBlank("#AjMwczM##ANyIDMwQDMwczM3YTM")){
        return null;
    }
    // 反转字符串
    String toString = new StringBuffer("#AjMwczM##ANyIDMwQDMwczM3YTM").reverse().toString();
    // 把 #号替换为=号
    String replaceAll = toString.replaceAll("#", "=");
    // 让==这个索引 + 6,获取索引,也就是最后四位
    int i = replaceAll.indexOf("==") + 6;
    // 截取刚刚得到的索引
    String substring = replaceAll.substring(i);
    // 解密
    String s = new String(Base64.getDecoder().decode(substring.getBytes()));
}

URL 解码

String decode = URLDecoder.decode("%23ATM1czM%23%23QMwgjNzgDO1czM3YTM", StandardCharsets.UTF_8);

正则

匹配手机号

String phone = "1((3[\\d])|(4[5-7|9])|(5[0-3|5-9])|(6[5-7])|(7[0-8])|(8[\\d])|(9[1|8|9]))\\d{8}";

"^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$" // 手机号

匹配邮箱

String s = "([0-9]|[a-z]|[A-Z])+@([0-9]|[a-z]|[A-Z])+\.([0-9]|[a-z]|[A-Z])+";

匹配身份证

String idCard = "^[1-9]\d{5}(18|19|20)\d{2}(0\d|10|11|12)(0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]";

集合

map 转 对象 import org.springframework.cglib.beans.BeanMap;

// 创建对象
IomBaseDataNe ne = new IomBaseDataNe(); 
// 交给bean map
BeanMap beanMap = BeanMap.create(ne);
// 开始复制
beanMap.putAll(map);

时间格式化

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月dd日");
        // 2025年11月07日
        System.out.println(formatter.format(now));

        // 解析
        LocalDate localdate = LocalDate.parse("2025年11月07日", formatter);
        System.out.println(localdate);

        //  时间
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("H:m:s");
        LocalTime time = LocalTime.parse("09:01:01", formatter2);
        System.out.println(time);

        // 完整日期+时间
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-M-d H:m:s");
        LocalDateTime localDateTime = LocalDateTime.parse("2025-11-07 09:01:01", formatter3);
        System.out.println(localDateTime);
    }

计算时间的相差

public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();

    // 生日时间
    LocalDateTime birthday = LocalDateTime.of(2025, 12, 13, 0, 0, 0);

    System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthday, now));
    System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthday, now));
    System.out.println("相差的周数" + ChronoUnit.WEEKS.between(birthday, now));
    System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthday, now));
    System.out.println("相差的小时数:" + ChronoUnit.HOURS.between(birthday, now));
    System.out.println("相差的分钟数:" + ChronoUnit.MINUTES.between(birthday, now));
    System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthday, now));
    System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthday, now));
    System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthday, now));
    System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthday, now));
    System.out.println("相差的半天数" + ChronoUnit.HALF_DAYS.between(birthday, now));
    System.out.println("相差的十年数" + ChronoUnit.DECADES.between(birthday, now));
    System.out.println("相差的世纪(百年)数" + ChronoUnit.CENTURIES.between(birthday, now));
    System.out.println("相差的千年数" + ChronoUnit.MILLENNIA.between(birthday, now));
    System.out.println("相差的纪元数" + ChronoUnit.ERAS.between(birthday, now));
}

线程池,JDK 提供的现有

队列,分为无界队列和有界队列,区别是,是否能指定队列长度。

  • LinkedBlockingQueue:无界,内部实现是int的最大值,其实也是有界的。
  • ArrayBlockingQueue:有界,需要自己指定队列长度。
  • SynchronousQueue:只能有一个任务存在,无缓冲区

newCachedThreadPool 线程池,会创建很大很大的线程,不建议使用。

newFixedThreadPool 需要自己指定线程池数量。但是不能控制临时线程。

public class Executors {

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
    
    public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}