java
复制代码
package com.bierce;
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.Date;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Zodiac;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil;
public class HutoolsTest {
public static void main(String[] args) {
//一. Hutool 工具包实践 https://github.com/itwanger/toBeBetterJavaer/blob/master/docs/common-tool/hutool.md
//1. 类型转换
String numStr = "22";
int num = Convert.toInt(numStr, 0); // 22
//2. Md5加密
String myPwd = "bishuai123";
String md5MyPwd = SecureUtil.md5().digestHex(myPwd); // 9cf7b74f75eb22be5af45ccfed7093a5
//3. 日期处理
String dateStr = "2020-09-29 22:33:23";
Date writeTextDate = DateUtil.parse(dateStr);
Date todayDate = DateUtil.date();
long betweenDay = DateUtil.between(writeTextDate, todayDate, DateUnit.MS);
String formatBetween = DateUtil.formatBetween(betweenDay, BetweenFormater.Level.SECOND);
System.out.println("formatBetween = " + formatBetween); //格式化时间差: 1359天19小时3分34秒
//星座和属相
String zodiac = Zodiac.getZodiac(DateUtil.parse("1995-03-18"));
System.out.println("星座 = " + zodiac); // 双鱼座
String chineseZodiac = Zodiac.getChineseZodiac(DateUtil.parse("1995-02-18"));
System.out.println("属相 = " + chineseZodiac); // 猪
//4. IO 流相关-网络操作和文件操作(文件目录的新建、删除、复制、移动、改名\判断文件或目录是否非空,是否为目录,是否为文件等)
BufferedInputStream in = FileUtil.getInputStream("hutool/origin.txt");
BufferedOutputStream out = FileUtil.getOutputStream("hutool/to.txt"); //默认输出项目路径:\target\test-classes
long copySize = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);
System.out.println("copySize = " + copySize);
//5. 字符串工具,和 Apache Commons Lang 包中的 StringUtils 类似
//6. 反射工具
// 构建对象
HutoolsTest hutoolsTest = ReflectUtil.newInstance(HutoolsTest.class);
System.out.println("hutoolTest = " + hutoolsTest);
// 获取构造方法
Constructor<HutoolsTest>[] constructors = ReflectUtil.getConstructors(HutoolsTest.class);
for (Constructor constructor : constructors) {
System.out.println(constructor.getName());
}
//7. 压缩工具
ZipUtil.zip("hutool", "hutool.zip");//默认输出项目路径:\target\test-classes
File unzip = ZipUtil.unzip("hutool.zip", "hutoolzip");//默认输出项目路径:\target\test-classes
//8. 身份证工具: 支持大陆 15 位、18 位身份证,港澳台 10 位身份证
String ID_18 = "321083197812162119";
boolean valid = IdcardUtil.isValidCard(ID_18);// 是否有效= true
//9. 控制台打印
Console.log("墨行子,一枚有趣的程序员");// 打印字符串 墨行子,一枚有趣的程序员
Console.log("西安是{}朝古都","13");// 打印字符串模板 西安是13朝古都
int [] ints = {1,2,3,4};
Console.log(ints);// 打印数组 [1, 2, 3, 4]
//10. 字段验证器(是不是邮箱/IP V4、V6/电话号码等等)
boolean isEmail = Validator.isEmail("墨行子"); // isEmail = false
boolean isMobile = Validator.isMobile("itwanger.com"); // isMobile = false
boolean isIPV4 = Validator.isIpv4("192.168.56.1"); // isIPV4 = true
//11. 图片工具( ImgUtil 可以对图片进行缩放、裁剪、转为黑白、加水印等操作)
ImgUtil.pressText(//
FileUtil.file("hutool/snow.jpg"),
FileUtil.file("hutool/snow2.jpg"),
"墨行子", Color.RED,
new Font("黑体", Font.BOLD, 50),
0,
0,
0.8f
); // 输出文件路径: \target\test-classes\hutool
//12. 加密解密(对称加密AES、DES,非对称RSA,摘要加密MD5、SHA-256等)
String encry = SecureUtil.md5().digestHex("墨行子"); //Md5加密: 8c4c11f5b8391b2eebbad4a9a0436e4e
//二. 其他常用工具包
//1. Apache commons工具包; 地址:https://github.com/itwanger/toBeBetterJavaer/blob/master/docs/common-tool/StringUtils.md
//2. guava 工具包; 地址:https://github.com/itwanger/toBeBetterJavaer/blob/master/docs/common-tool/guava.md
//3. 其他常用Java工具类:IpUtil、MDC、ClassUtils、BeanUtils、ReflectionUtils; 地址:https://github.com/itwanger/toBeBetterJavaer/blob/master/docs/common-tool/utils.md
}
}