超好用的工具 -- hutool(难得糊涂)

24 篇文章 1 订阅
在这里插入图片描述


在这里插入图片描述
在这里插入图片描述

Git Website: https://github.com/dromara/hutool.

Official website: https://www.hutool.cn/.


1. Introduction

1.1 show

在这里插入图片描述

1.2 Components

模块介绍
hutool-aopJDK动态代理封装,提供非IOC下的切面支持
hutool-bloomFilter布隆过滤,提供一些Hash算法的布隆过滤
hutool-cache简单缓存实现
hutool-core核心,包括Bean操作、日期、各种Util等
hutool-cron定时任务模块,提供类Crontab表达式的定时任务
hutool-crypto加密解密模块,提供对称、非对称和摘要算法封装
hutool-dbJDBC封装后的数据操作,基于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-jsonJSON实现
hutool-captcha图片验证码实现
hutool-poi针对POI中Excel和Word的封装
hutool-socket基于Java的NIO和AIO的Socket封装
hutool-jwtJSON 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

         在一秒钟内看到本质的人和花半辈子也看不清一件事本质的人,自然是不一样的命运。
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用企业微信提供的API来获取通讯录信息。具体步骤如下: 1. 在企业微信后台中创建一个应用,并获取应用的`CorpID`、`Secret`和`AgentID`。 2. 通过企业微信提供的API获取`access_token`,用于后续的接口调用。具体接口为: ``` https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET ``` 其中,`ID`为企业的`CorpID`,`SECRET`为应用的`Secret`。 3. 获取部门列表。调用以下接口: ``` https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID ``` 其中,`ACCESS_TOKEN`为上一步获取的`access_token`,`ID`为部门ID,默认获取根部门下的所有部门信息。 4. 获取部门成员列表。调用以下接口: ``` https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD ``` 其中,`ACCESS_TOKEN`为上一步获取的`access_token`,`DEPARTMENT_ID`为部门ID,`FETCH_CHILD`为是否递归获取子部门成员(1表示获取,0表示不获取)。 5. 获取成员详细信息。调用以下接口: ``` https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID ``` 其中,`ACCESS_TOKEN`为上一步获取的`access_token`,`USERID`为成员的UserID。 通过以上接口调用,即可获取企业微信的通讯录信息。需要注意的是,调用接口时需要将参数进行url编码,并且需要使用HTTPS协议。同时,企业微信提供了SDK和开发文档,可以参考官方文档进行开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世经纶『一页書』

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值