• 生成uuid 和 检验


    //注意replaceAll前面的是正则表达式
    String uuid = UUID.randomUUID().toString().replaceAll("-","");
    System.out.println(uuid);
    // System.out.println(uuid.length());

    时间戳:System.currentTimeMillis()

    System.out.println(System.currentTimeMillis());

    //下面是uuid的生产和检验

    public static void main(String[] args) {
    // String uuid1 = "e65deb4c-a110-49c8-a4ef-6e69447968d6";
    // String uuid2 = "ca4a8a92-d4ed-4fc4-8a4f-345c587fbdcb";
    // String uuid3 = "e1f15f1d-6edb-4f70-8a05465se273eaf95a";
    // System.out.println("check > " + uuid1 + " > " + isValidUUID(uuid1));
    // System.out.println("check > " + uuid2 + " > " + isValidUUID(uuid2));
    // System.out.println("check > " + uuid3 + " > " + isValidUUID(uuid3));
    // System.out.println("build a uuid> " + getRandomUUID(null));
    // System.out.println("build a uuid> " + getRandomUUID(null));
    // System.out.println("build a uuid> " + getRandomUUID("kangyucheng"));
    // System.out.println("build a uuid> " + getRandomUUID("kangyucheng"));

    // String str = "88c2319548484ab790cc063376c097e1";

    String str = "88c23195-4848-4ab7-90cc-063376c097e1";
    System.out.println(str.length());

    System.out.println("check > " + str + " > " + isValidUUID(str));

    }

    public static boolean isValidUUID(String uuid) {
    // UUID校验
    if (uuid == null) {
    System.out.println("uuid is null");
    }
    String regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
    if (uuid.matches(regex)) {
    return true;
    }
    return false;
    }

    public static UUID getRandomUUID(String str) {
    // 产生UUID
    if (str == null) {
    return UUID.randomUUID();
    } else {
    return UUID.nameUUIDFromBytes(str.getBytes());
    }
    }

  • 相关阅读:
    浅谈 unix, linux, ios, android 区别和联系
    比 Java 更具争议的 PHP,处处留坑?
    JavaScript 对象
    前端:for、foreach、for in、for of、$.each、$().each的区别
    java按照关键字指定的key删除redis(支持模糊删除)
    单点登录SSO
    页面打印的总结
    Jquery对selec操作的总结
    JS判断操作系统
    动态生成js的正则,替换字符串
  • 原文地址:https://www.cnblogs.com/runerering/p/11505833.html
Copyright © 2020-2023  润新知