学习Java的Hutool工具类库(1)

网友投稿 1157 2022-05-30

在我们开发Java项目的过程中,JDK的有些功能并没有,经常会有一些特殊的内容要处理,比如:日期和时间处理,字符串、数字处理,生成二维码等功能,如果自已一个一个写不仅浪费时间,还要考虑效率可靠性。如果有现成一个成熟的工具类库,那我们使用它何乐而不为呢,还能提高效率节省时间。

hutool简介

Hutool是一个小而全的Java工具类库,通过静态方法封装,它帮助我们简化每一行代码,减少每一个方法,将一些通用类和方法提炼出来,在项目中引用 jar 包后,促使我们拿来就用,从而提高工作效率。

模块功能

它是一个 Java 基础工具类,对文件、流、加密解密、转码、正则、线程、XML 等 JDK 方法进行封装,组成各种 Util 工具类,同时提供以下组件:

hutool-aop: JDK动态代理封装,提供非IOC下的切面支持。

hutool-bloomFilter: 布隆过滤,提供一些Hash算法的布隆过滤。

hutool-cache: 简单缓存实现。

hutool-core: 核心,包括Bean操作,日期,各种Util等等。

hutool-cron: 定时任务模块,提供类Crontab表达式的定时任务。

hutool-crypto: 加密解密模块,提供对称,非对称和摘要算法封装。

hutool-dfa: 基于DFA模型的多关键字查找。

hutool-db: JDBC封装后的数据操作,基于ActiveRecord思想。

hutool-extra: 扩展模块,对第三方封装(模板引擎,邮件,Servlet,二维码,Emoji,FTP,分词等)。

hutool-http: 基于HttpUrlConnection的Http客户端封装。

hutool-log: 自动识别日志实现的日志门面。

hutool-script: 脚本执行封装,如:JavaScript。

hutool-setting: 功能更强大的Setting配置文件和Properties封装。

hutool-system: 系统参数调用封装(JVM信息等)。

hutool-json: JSON实现。

hutool-captcha: 图片验证码实现。

hutool-poi: 针对POI中Excel和Word的封装。

hutool-socket: 基于Java的NIO和AIO的Socket封装。

hutool-jwt:JSON Web Token (JWT)封装实现。

我们主要常用:hutool-core,hutool-crypto,hutool-extra,hutool-http,hutool-captcha

安装方式

1.Maven

在项目的 pom.xml 的 dependencies 中加入以下内容

cn.hutool hutool-all 5.7.15

2.Gradle

implementation 'cn.hutool:hutool-all:5.7.15'

3.引用 jar 包

下载地址:https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.7.15/

项目工程右键->Build Path(构建路径)->Add External Archives...(添加外部归档),选中下载的“hutool-all-5.7.15.jar”文件。

使用hutool-core

通过引入 hutool-all 方式引入所有模块,也可以根据需求单独引入使用的模块。

日期和时间

引入:import cn.hutool.core.date.DateUtil;

public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 获取当前时间 Date date = DateUtil.date(); System.out.println(date); // 当前时间,格式:yyyy-MM-dd HH:mm:ss String now = DateUtil.now(); System.out.println(now); // 当前日期,格式:yyyy-MM-dd String today= DateUtil.today(); System.out.println(today); // 字符串转日期类型 String dateStr = "2021-11-06"; Date date1 = DateUtil.parse(dateStr); System.out.println(date1); } }

字符串

引入:import cn.hutool.core.util.StrUtil;

public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 字符串处理 String str = "hello world"; // hasEmpty只判断是否为null或者空字符串,返回 true/false System.out.println(StrUtil.hasEmpty(str)); // 截取字符串,sub(字符串,起始位置索引,结束位置索引) String strSub1 = StrUtil.sub(str, 1, 3); System.out.println(strSub1); // 字符串模板,格式化 String str1 = "hello {}"; String str2 = StrUtil.format(str1, "world"); System.out.println(str2); } }

类型转换

引入:import cn.hutool.core.convert.Convert;

public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 类型转换 // 数字转换字符串 int a = 1; String aStr = Convert.toStr(a); // 转换为日期对象 String b = "2017-05-06"; Date value = Convert.toDate(b); // 转换为集合 Object[] c = {3, "1", "hello", ""}; List list = Convert.toList(c); System.out.println(list); } }

学习Java的Hutool工具类库(1)

使用hutool-captcha

图形验证码

引入:import cn.hutool.captcha.CaptchaUtil;

import cn.hutool.captcha.LineCaptcha;

public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 图形验证码 // 定义图形验证码的长和宽 LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100); // 图形验证码写出,可以写出到文件,也可以写出到流 lineCaptcha.write("d:/line.png"); // 输出code System.out.println(lineCaptcha.getCode()); // 验证图形验证码的有效性,返回 boolean 值 System.out.println(lineCaptcha.verify("abcd")); } }

注:关于 Hutool API 更多的学习内容,请参考官方文档:https://hutool.cn/docs/#/

温馨提示

文章内容如果写的存在问题欢迎留言指出,让我们共同交流,共同探讨,共同进步~~~

文章如果对你有帮助,动动你的小手点个赞,鼓励一下,给我前行的动力。

Java

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:VS Code常用快捷键
下一篇:jQuery之高级选择器
相关文章