Fork me on GitLab

Hutool工具使用

Hutool工具使用

介绍  

 

在大家日常工作中,都常常会做如下这些非常繁琐的工作。

这个是作者根据https://how2j.cn/k/hutool/hutool-url/1952.html#nowhere大佬的文档做的笔记。

开始

导入maven包

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.5</version>
        </dependency>
                
public static void main(String[] args) {
     // Date 和 String之间转换 String dateStr
= "2012-12-12 12:12:12"; Date date = DateUtil.parse(dateStr); System.out.println(date); }

十六进制编码工具

 //判断是否是十六进制
//        String s1 = "12";
//        boolean b1 = HexUtil.isHexNumber(s1);
//        String s2 = "0x12";
//        boolean b2 = HexUtil.isHexNumber(s2);
//
//        System.out.println("字符串" + s1+ "是否十六机制"+b1);
//        System.out.println("字符串" +s2+ "是否十六机制"+b2);

        //颜色转换 可以将# 转换为rgb
        Color color1 = Color.red;
        String s1 = HexUtil.encodeColor(color1);
        String s2 = "#112233";
        Color color2 = HexUtil.decodeColor(s2);
        System.out.println("颜色对象1"+color1+ "字符串"+s1);
        System.out.println("字符串"+s2+ "颜色对象2"+color2);

转义工具

//转义与反转义
        String s1 = "<script>location.href='http://how2j.cn';</script>";
        String s2 = EscapeUtil.escapeHtml4(s1);
        String s3 = EscapeUtil.unescapeHtml4(s2);
        System.out.println("源数据" + s1 + "转意后" + s2);
        System.out.println("源数据" + s2 + "转意后" + s3);

运行控制台输出

 

 Hash工具

   // hash算法
        String s = "hello word!";
        int number = 12;
        long hash1 = HashUtil.additiveHash(s, Integer.MAX_VALUE);
        long hash2 = HashUtil.rotatingHash(s, Integer.MAX_VALUE);
        long hash3 = HashUtil.oneByOneHash(s);
        long hash4 = HashUtil.bernstein(s);
        long hash7 = HashUtil.fnvHash(s);
        long hash8 = HashUtil.intHash(number);
        long hash9 = HashUtil.rsHash(s);
        long hash10 = HashUtil.jsHash(s);
        long hash11 = HashUtil.pjwHash(s);
        long hash12 = HashUtil.elfHash(s);
        long hash13 = HashUtil.bkdrHash(s);
        long hash14 = HashUtil.sdbmHash(s);
        long hash15 = HashUtil.djbHash(s);
        long hash16 = HashUtil.dekHash(s);
        long hash17 = HashUtil.apHash(s);
        long hash18 = HashUtil.tianlHash(s);
        long hash19 = HashUtil.javaDefaultHash(s);
        long hash20 = HashUtil.mixHash(s);
        System.out.println("源数据" + s + "加法算法对应的哈希值" + hash1);
        System.out.println("源数据" + s + "旋转算法对应的哈希值" + hash2);
        System.out.println("源数据" + s + "一次一个算法对应的哈希值" + hash3);
        System.out.println("源数据" + s + "Bernstein's算法对应的哈希值" + hash4);
        System.out.println("源数据" + s + "改进的32位FNV 算法对应的哈希值" + hash7);
        System.out.println("源数据" + s + "Thomas Wang的整数算法对应的哈希值" + hash8);
        System.out.println("源数据" + s + "RS算法对应的哈希值" + hash9);
        System.out.println("源数据" + s + "JS算法对应的哈希值" + hash10);
        System.out.println("源数据" + s + "PJ算法对应的哈希值" + hash11);
        System.out.println("源数据" + s + "ELF算法对应的哈希值" + hash12);
        System.out.println("源数据" + s + "BKDR算法对应的哈希值" + hash13);
        System.out.println("源数据" + s + "SDBM算法对应的哈希值" + hash14);
        System.out.println("源数据" + s + "DJB算法对应的哈希值" + hash15);
        System.out.println("源数据" + s + "DEK算法对应的哈希值" + hash16);
        System.out.println("源数据" + s + "AP算法对应的哈希值" + hash17);
        System.out.println("源数据" + s + "TianL算法对应的哈希值" + hash18);
        System.out.println("源数据" + s + "JAVA自己带算法对应的哈希值" + hash19);
        System.out.println("源数据" + s + "混合算法对应的哈希值" + hash20);
View Code

运行控制台输出

"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:D:\java\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar=65217:D:\java\IntelliJ IDEA 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_261\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\rt.jar;D:\project\get1\gitLabel\shar\demo\target\classes;D:\java\Maven\apache-maven-3.6.3-bin\repository\cn\hutool\hutool-all\5.3.5\hutool-all-5.3.5.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter\2.4.1\spring-boot-starter-2.4.1.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot\2.4.1\spring-boot-2.4.1.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-context\5.3.2\spring-context-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.1\spring-boot-autoconfigure-2.4.1.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-logging\2.4.1\spring-boot-starter-logging-2.4.1.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-core\5.3.2\spring-core-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-jcl\5.3.2\spring-jcl-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-web\2.4.0\spring-boot-starter-web-2.4.0.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-json\2.4.1\spring-boot-starter-json-2.4.1.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.1\spring-boot-starter-tomcat-2.4.1.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-web\5.3.2\spring-web-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-beans\5.3.2\spring-beans-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-webmvc\5.3.2\spring-webmvc-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-aop\5.3.2\spring-aop-5.3.2.jar;D:\java\Maven\apache-maven-3.6.3-bin\repository\org\springframework\spring-expression\5.3.2\spring-expression-5.3.2.jar" com.example.demo.IOOpeation
源数据hello word!加法算法对应的哈希值1052
源数据hello word!旋转算法对应的哈希值1385059675
源数据hello word!一次一个算法对应的哈希值-998279044
源数据hello word!Bernstein's算法对应的哈希值1239941009
源数据hello word!改进的32位FNV 算法对应的哈希值1699685277
源数据hello word!Thomas Wang的整数算法对应的哈希值-321385985
源数据hello word!RS算法对应的哈希值306546121
源数据hello word!JS算法对应的哈希值1584115779
源数据hello word!PJ算法对应的哈希值18588753
源数据hello word!ELF算法对应的哈希值18588753
源数据hello word!BKDR算法对应的哈希值1310282217
源数据hello word!SDBM算法对应的哈希值430342793
源数据hello word!DJB算法对应的哈希值894551926
源数据hello word!DEK算法对应的哈希值2090529829
源数据hello word!AP算法对应的哈希值1868402362
源数据hello word!TianL算法对应的哈希值169635427
源数据hello word!JAVA自己带算法对应的哈希值1794105737
源数据hello word!混合算法对应的哈希值7705625467680662429

Process finished with exit code 0

URL工具

   // URLUtil使用举例
        String url1 = "baidu.cn";
        String url2 = "http://baidu.com";
        String urlb = URLUtil.encode(url2);
        String urlc = URLUtil.decode(urlb);
        String urld = URLUtil.getPath(url2);

        p1("原数据  ",url2, "编码数据    ",urlb);
        p1("编码数据",urlb, "解码数据    ",urlc);
        p1("原数据  ",url2, "对应路径路径",urld);

base-32-64转换工具

// base-32-64转换
        String charset = "utf-8";
        String content = "Hello Word!";
        p3("原字符串",content);
        String code32= Base32.encode(content, charset);
        content = Base32.decodeStr(code32,charset);
        p3("32位编码后",code32);
        p3("32位解码",content);
        String code64= Base64.encode(content, charset);
        content = Base64.decodeStr(code64,charset);
        p3("64位编码后",code64);
        p3("64位解码",content);

运行控制台输出

 

 Unicode工具

  String charset = "utf-8";
        String content = "hello word";
        p3("原字符串",content);

        String unicode= UnicodeUtil.toUnicode(content);
        content = UnicodeUtil.toString(unicode);

        p3("获取unicode",unicode);
        p3("转会原字符串",content);

转换工具

// 转换为字符串
        int a = 1;
        String aStr = Convert.toStr(a);
        int[] b = {1,2,3,4,5};
        String bStr = Convert.toStr(b);
        Object c = null;
        String cStr = Convert.toStr(c,"空字符串(默认值)");
        p1("整数", a, "字符串" , aStr);
        p1("long数组", b, "字符串" , bStr);
        p1("空对象", c, "字符串" , cStr);
        // 数组类型互相转化
        String[] a = { "1", "2", "3", "4" };
        Integer[] b = Convert.toIntArray(a);
        p1("字符串数组", Convert.toStr(a), "Integer数组" , Convert.toStr(b));
        // 数组和集合互换
        String[] a = { "1", "2", "3", "4" };
        List<?> l = Convert.toList(a);
        String[] b= Convert.toStrArray(l);
        p1("字符串数组", a, "集合" , l);
        p1("集合", l, "字符串数组" , b);
        // 半角全角互相转换
        String a = "123456789";
        String b = Convert.toSBC(a);
        String c = Convert.toDBC(b);
        p1("半角", a, "全角" , b);
        p1("全角", b, "半角" , c);
        // Unicode和字符串转换
        String a = "how2j的Hutool教程";
        String unicode = Convert.strToUnicode(a);
        String b = Convert.unicodeToStr(unicode);

        p1("字符串", a, "unicode" , unicode);
        p1("unicode", unicode, "字符串" , b);
        // 不同编码之间的转换
        String a = "how2j的Hutool教程";
        //转换后result为乱码
        String b = Convert.convertCharset(a, CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1);
        String c = Convert.convertCharset(b, CharsetUtil.ISO_8859_1, "UTF-8");
        p1("UTF-8", a, "IOS-8859-1" , b);
        p1("IOS-8859-1", b, "UTF-8" , c);
        // 数字转换为金额
        double  a= 1234567123456.12;
        String b = Convert.digitToChinese(a);
        p1("数字", a, "钞票金额" , b);
        // 原始类和包装类转换
        Class<?> wrapClass = Integer.class;
        Class<?> unWraped = Convert.unWrap(wrapClass);
        Class<?> primitiveClass = long.class;
        Class<?> wraped = Convert.wrap(primitiveClass);
        p1("包装类型", wrapClass, "原始类型" , unWraped);
        p1("原始类型", primitiveClass, "wraped" , wraped);
View Code

 日期工具

public static void main(String[] args) {
        test0();
        test1();
        test3();
    }

    public static void test0() {
        // 字符串转日期
        Date d;
        String str3 = "12:12:12";
        d = DateUtil.parse(str3);
        p1("字符串",str3, "日期格式",d);

        String str1 = "2012-12-12";
        d = DateUtil.parse(str1);
        p1("字符串",str1, "日期格式",d);

        String str4 = "2012-12-12 12:12";
        d = DateUtil.parse(str4);
        p1("字符串",str4, "日期格式",d);

        String str2 = "2012-12-12 12:12:12";
        d = DateUtil.parse(str2);
        p1("字符串",str2, "日期格式",d);
    }

    public static void test1() {
        // 日期转字符串
        Date d = new Date();
        //结果 2017/03/01
        String format = DateUtil.format(d, "yyyy/MM/dd");
        //常用格式的格式化,结果:2017-03-01
        String formatDate = DateUtil.formatDate(d);
        //结果:2017-03-01 00:00:00
        String formatDateTime = DateUtil.formatDateTime(d);
        //结果:00:00:00
        String formatTime = DateUtil.formatTime(d);
        p1("日期格式",d, "自定义格式的字符串",format);
        p1("日期格式",d, "只是日期格式",formatDate);
        p1("日期格式",d, "日期和时间格式",formatDateTime);
        p1("日期格式",d, "只是时间格式",formatTime);
    }

    public static void test3() {
        // 开始和结束时间
        Date date = new Date();
        //一天的开始,结果:2017-03-01 00:00:00
        Date beginOfDay = DateUtil.beginOfDay(date);

        //一天的结束,结果:2017-03-01 23:59:59
        Date endOfDay = DateUtil.endOfDay(date);
        p1("当前日期",DateUtil.formatDateTime(date), "开始时间",beginOfDay);
        p1("当前日期",DateUtil.formatDateTime(date), "结束时间",endOfDay);
    }
View Code

 

posted @ 2020-12-25 16:51  隐琳琥  阅读(3114)  评论(0编辑  收藏  举报