![在这里插入图片描述](https://img-blog.csdnimg.cn/9633f3bb7c3643d0a6989e51c0470ac6.gif#pic_center)
超好用的工具 -- hutool(难得糊涂)
![](https://img-blog.csdnimg.cn/img_convert/7b0d6ba5dcce6ce6b2e732fdffde6496.gif)
Git Website: https://github.com/dromara/hutool.
Official website: https://www.hutool.cn/.
1. Introduction
1.1 show
1.2 Components
模块 | 介绍 |
---|---|
hutool-aop | JDK动态代理封装,提供非IOC下的切面支持 |
hutool-bloomFilter | 布隆过滤,提供一些Hash算法的布隆过滤 |
hutool-cache | 简单缓存实现 |
hutool-core | 核心,包括Bean操作、日期、各种Util等 |
hutool-cron | 定时任务模块,提供类Crontab表达式的定时任务 |
hutool-crypto | 加密解密模块,提供对称、非对称和摘要算法封装 |
hutool-db | JDBC封装后的数据操作,基于ActiveRecord思想 |
hutool-dfa | 基于DFA模型的多关键字查找 |
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)封装实现 |
2. Code
2.1 Maven import
- 需要jdk8及以上
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version>
</dependency>
2.2 Api
Api Document: https://apidoc.gitee.com/dromara/hutool/.
2.2.1 时间日期
// 获取年份
int year = DateUtil.year(new Date());
// 获取今天日期 yyyy-MM-dd格式
String today = DateUtil.today();
// 获取生肖
String chineseZodiac = DateUtil.getChineseZodiac(1990);
// 将毫秒转成方便阅读的时间,如3小时25分23秒232毫秒
String readableTime = DateUtil.formatBetween(12323232);
// 转为农历日期
ChineseDate chineseDate = new ChineseDate(new Date());
// 农历年份,如2021
final int chineseYear = chineseDate.getChineseYear();
// 农历月份,如腊月
final String chineseMonthName = chineseDate.getChineseMonthName();
// 农历日期,如初三
final String chineseDay = chineseDate.getChineseDay();
// 方便地将Date转换为LocalDateTime
final LocalDateTime localDateTime = LocalDateTimeUtil.of(new Date());
// 获取一天开始时间
LocalDateTimeUtil.beginOfDay(localDateTime);
// 获取一天结束时间
LocalDateTimeUtil.endOfDay(localDateTime);
2.2.2 IO流
// 从文件中获取缓冲流
BufferedInputStream in = FileUtil.getInputStream("d:/test.txt");
BufferedOutputStream out = FileUtil.getOutputStream("d:/test2.txt");
// 拷贝文件
IoUtil.copy(in, out);
2.2.3 字符串处理
// 判断字符串是否为null或空串
boolean isEmpty = StrUtil.isEmpty(str);
// 判断字符串是否为null或空串或空白字符
boolean isBlank = StrUtil.isBlank(str);
// 将字符串用指定字符填充到指定长度
String filled = StrUtil.fillAfter(str, '*', 10);
// 填充字符串模板
String format = StrUtil.format("a的值为{a}, b的值为{b}", Map.of("a", "aValue", "b", "bValue"));
// 判断字符串是否为中文字符串
boolean match = ReUtil.isMatch(ReUtil.RE_CHINESES, "中国人");
2.2.4 集合
// 新建一个HashSet
Set<Integer> hashSet = CollUtil.newHashSet(1, 2, 3);
Set<Integer> linkedHashSet = CollUtil.newLinkedHashSet(4, 2, 3);
// 两个集合取交集
Collection<Integer> intersection = CollUtil.intersection(hashSet, linkedHashSet);
// 两个集合取并集
Collection<Integer> union = CollUtil.union(hashSet, linkedHashSet);
// 两个集合取差集
Collection<Integer> disjunction = CollUtil.disjunction(hashSet, linkedHashSet);
// 判断一个集合是否为null或空集
boolean empty = CollUtil.isEmpty(hashSet);
// 创建一个ArrayList
List<Integer> arrayList = ListUtil.toList(1, 2, 3);
// 创建一个LinkedList
List<Integer> linkedList = ListUtil.toLinkedList(1, 2, 3);
// 创建一个map
Map<String, Object> map = MapUtil.<String, Object>builder().put("a", 1).put("b", 2).build();
2.2.5 HTTP请求
Map<String, Object> params = MapUtil.<String, Object>builder().put("a", 1).build();
// 发送get请求
String getResult = HttpUtil.get("https://www.baidu.com", params);
// 发送post请求
String postResult = HttpUtil.post("https://www.baidu.com", params);
// 以application/json方式发送post请求
String jsonPostResult = HttpUtil.post("https://www.baidu.com", JSON.toJSONString(params));
2.2.6 加密
// md5摘要加密
String md5 = SecureUtil.md5("abc");
// sha1摘要加密
String sha1 = SecureUtil.sha1("abc");
// 生成非对称密钥对
KeyPair keyPair = SecureUtil.generateKeyPair("RSA");
String publicKey = Base64Encoder.encode(keyPair.getPublic().getEncoded());
String privateKey = Base64Encoder.encode(keyPair.getPrivate().getEncoded());
// 利用公钥加密
String encryptBase64 = SecureUtil.rsa(privateKey, publicKey).encryptBase64("abc", KeyType.PublicKey);
// 利用私钥解密
String decrypt = new String(SecureUtil.rsa(privateKey, publicKey).decrypt(encryptBase64, KeyType.PrivateKey));
// 创建签名对象
Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
// 生成签名
final byte[] bytes = "abc".getBytes();
byte[] signed = sign.sign(bytes);
// 验证签名
boolean verify = sign.verify(bytes, signed);
System.out.println(verify);
2.2.7 其他
// 根据身份证号获取出生日期
String birth = IdcardUtil.getBirthByIdCard(idCard);
// 根据身份证号获取省份
String province = IdcardUtil.getProvinceByIdCard(idCard);
// 判断身份证号是否合法
boolean valid = IdcardUtil.isValidCard18(idCard);
// 获取一个随机的社会信用代码
String creditCode = CreditCodeUtil.randomCreditCode();
// 判断社会信用代码是否合法
boolean isCreditCode = CreditCodeUtil.isCreditCode(creditCode);
// 将汉字转为拼音,需要引入TinyPinyin、JPinyin或Pinyin4j的jar包
String china = PinyinUtil.getPinyin("中国");
// 将字符串生成为二维码,需要引入com.google.zxing.core的jar包
BufferedImage qrCodeImage = QrCodeUtil.generate("www.baidu.com", QrConfig.create());
ImageIO.write(qrCodeImage, "png", new File("a.png"));
// 生成uuid
String uuid = IdUtil.fastSimpleUUID();
// 创建基于Twitter SnowFlake算法的唯一ID,适用于分布式系统
final Snowflake snowflake = IdUtil.createSnowflake(1, 1);
final long id = snowflake.nextId();
3.Awakening
在一秒钟内看到本质的人和花半辈子也看不清一件事本质的人,自然是不一样的命运。