hutool-patterns
|
|
来源:https://doc.hutool.cn/
GitHub:https://github.com/chinabugotech/hutool
| 场景 | Hutool | Guava | Apache Commons | Java 标准库 | |------|:-----:|:-----:|:--------------:|:----------:| | 字符串处理 | ⭐⭐ | ⭐⭐ | ⭐⭐⭐ | ⭐ | | 日期处理 | ⭐⭐⭐ | — | ⭐ | ⭐⭐ | | HTTP客户端 | ⭐⭐⭐(简单) | — | ⭐⭐ | ⭐⭐ | | 身份证/手机号 | ⭐⭐⭐ | — | ⭐⭐ | — | | 加解密 | ⭐⭐ | — | ⭐ | — | | 文件操作 | ⭐⭐⭐ | ⭐ | ⭐⭐⭐ | ⭐⭐ | | JSON | ⭐⭐ | — | — | — | | 集合操作 | ⭐⭐ | ⭐⭐⭐ | ⭐⭐ | ⭐⭐ |
| # | 错误 | 正确做法 |
|---|------|---------|
| 1 | 手写身份证校验正则 30 行 | IdcardUtil.isValidCard("440101...") — 含校验位算法 |
| 2 | String.format() 用 %s 难读 | StrUtil.format("Hello {}", name) 命名占位更可读 |
| 3 | 手写日期格式转换/计算 | DateUtil.parse(dateStr).offset(DateField.DAY_OF_MONTH, 7) |
| 4 | Apache HttpClient 50行代码发GET | HttpUtil.get(url) 一行搞定 |
| 5 | isEmpty 判断空白字符串误判 | 用 StrUtil.isBlank() 替代 isEmpty() — 含空白判断 |
| 6 | 高并发场景也用 HttpUtil | 高并发用 OkHttp 连接池,HttpUtil 仅简单场景 |
| 7 | BeanUtil.copyProperties 替代 MapStruct | BeanUtil 是反射(运行时慢),MapStruct 是编译期生成(快) |
| 8 | JSONUtil 替代 Jackson | JSONUtil 仅轻量场景,生产 API 用 Jackson |
| 9 | StrUtil.isEmpty(" ") 返回 false | StrUtil.isBlank(" ") 返回 true |
| 10 | SimpleDateFormat 线程不安全 | DateUtil.parse() 线程安全,底层 ThreadLocal |
// ✅ 字符串—isBlank 优于 isEmpty
StrUtil.isBlank(null); // true ✅
StrUtil.isBlank(""); // true
StrUtil.isBlank(" "); // true ← isEmpty 认为 false
StrUtil.isEmpty(" "); // false ❌
// ✅ 字符串—命名占位比 %s 可读
StrUtil.format("Hello {}, 欢迎来到{}!", "张三", "上海");
StrUtil.format("你好, {name}, 欢迎来到{city}!",
MapUtil.of("name", "张三", "city", "上海"));
// ✅ 字符串—截取中间文本
String content = StrUtil.subBetween("prefix_content_suffix", "prefix_", "_suffix"); // "content"
StrUtil.removePrefix("prefix_content", "prefix_"); // "content"
StrUtil.removeSuffix("content_suffix", "_suffix"); // "content"
// ✅ 字符串—分割(自动 trim、忽略空值)
List<String> parts = StrUtil.split("a,b, c ,d", ',', 0, true, true);
// => ["a", "b", "c", "d"]
// ✅ 日期—自动识别多种格式
DateUtil.parse("2025-08-04"); // yyyy-MM-dd
DateUtil.parse("2025/08/04 10:30:00"); // yyyy/MM/dd HH:mm:ss
DateUtil.parse("2025年08月04日"); // 中文日期
// ✅ 日期—计算与比较
DateUtil.offsetDay(new Date(), 7); // 7天后
DateUtil.between(begin, end, DateUnit.DAY); // 间隔天数
DateUtil.beginOfMonth(new Date()); // 月初
DateUtil.endOfMonth(new Date()); // 月末
// ✅ HTTP—简单请求一行搞定
String result = HttpUtil.get("https://api.example.com/data");
HttpUtil.post(url, params); // application/x-www-form-urlencoded
HttpUtil.post(url, jsonBody); // JSON POST
// ✅ HTTP—链式调用(自定义Header/超时)
String result = HttpUtil.createPost(url)
.header("Authorization", "Bearer xxx")
.header("Content-Type", "application/json")
.timeout(5000) // 5秒超时
.body(jsonBody)
.execute()
.body();
// ✅ 身份证—全套处理
IdcardUtil.isValidCard("440101199001011234"); // 校验含校验位
int age = IdcardUtil.getAgeByIdCard(idCard); // 提取年龄
String gender = IdcardUtil.getGenderByIdCard(idCard); // 1=男, 0=女
String birth = IdcardUtil.getBirthByIdCard(idCard); // 19900101
String province = IdcardUtil.getProvinceByIdCard(idCard); // 广东
// ✅ 数据脱敏
DesensitizedUtil.idCardNum(idCard, 1, 2); // 440***********1234
DesensitizedUtil.mobilePhone("13800138000"); // 138****8000
DesensitizedUtil.email("[email protected]"); // t***@example.com
// ✅ 校验
Validator.isMobile("13800138000");
Validator.isEmail("[email protected]");
Validator.isCitizenId("440101199001011234"); // 轻量版身份证校验
// ✅ 集合
CollUtil.isEmpty(list); // 安全的判空(null或空集)
List<String> union = CollUtil.union(list1, list2); // 并集
List<String> inter = CollUtil.intersection(list1, list2); // 交集
Map<String, List<User>> grouped = CollUtil.groupBy(users, User::getStatus); // 分组
// 1. 快速格式校验 → 2. 完整校验位验证 → 3. 提取信息
if (Validator.isCitizenId(idCard)) { // 轻量正则
if (IdcardUtil.isValidCard(idCard)) { // 完整校验(含校验位)
int age = IdcardUtil.getAgeByIdCard(idCard);
String gender = IdcardUtil.getGenderByIdCard(idCard);
}
}
// 查询外部 API 时缓存空值,防止穿透
public String queryWeather(String city) {
return cache.get(city, key -> {
String result = HttpUtil.get("https://api.weather.com/" + city);
if (StrUtil.isBlank(result)) {
return "NULL_CACHE"; // 空值标记,缓存5分钟
}
return result;
});
}
String clean = StrUtil.trim(input); // 去首尾空白
String named = StrUtil.toCamelCase(clean); // 转驼峰
List<String> parts = StrUtil.split(named, ','); // 按逗号分割
本技能不收集、存储或传输任何用户数据。