| name | hutool-patterns |
| description | Hutool 中文 Java 工具库技能。覆盖与 Guava/Apache Commons 的功能选择矩阵、字符串/集合/日期/文件/HTTP 最常用方法速查、HttpUtil vs OkHttp 选型、BeanUtil vs MapStruct 职责划分。
当用户在 Java 项目中需要中文场景的工具方法(拼音/身份证/手机号校验)、HTTP请求、文件操作、加解密时使用。避免 LLM 自造轮子而不用 Hutool。
|
| license | Apache-2.0 |
Hutool Java 工具库
来源:https://doc.hutool.cn/
GitHub:https://github.com/chinabugotech/hutool
Capability Boundaries
✅ Strong Suits
- StrUtil — format/空判断/截断/下划线驼峰互转、subBetween 截取中间文本
- CollUtil — isEmpty/intersection/union/subtract/groupBy 集合操作
- DateUtil — 日期格式化/解析(自动识别多格式)/偏移/时间差/计时器
- HttpUtil — HTTP GET/POST/文件上传(简化 OkHttp/Apache HttpClient)
- JSONUtil — 轻量 JSON 序列化(非高吞吐场景)
- SecureUtil — MD5/SHA/AES/RSA/Base64 简化版
- IdcardUtil — 身份证校验(支持15/18位)、提取生日/性别/年龄/省份
- Validator — 手机号/邮箱/IP/身份证/URL 校验
- DesensitizedUtil — 数据脱敏(手机号/身份证/银行卡/地址)
- ReUtil — 正则匹配/提取/替换简化
❌ Out of Scope
- 高性能 JSON → 用 Jackson/Fastjson2(Spring Boot 默认)
- HTTP 高并发 → 用 OkHttp 连接池
- Bean 映射 → 用 MapStruct 编译期生成
- 分布式锁 → 用 Redisson
工具选择矩阵
| 场景 | Hutool | Guava | Apache Commons | Java 标准库 |
|---|
| 字符串处理 | ⭐⭐ | ⭐⭐ | ⭐⭐⭐ | ⭐ |
| 日期处理 | ⭐⭐⭐ | — | ⭐ | ⭐⭐ |
| HTTP客户端 | ⭐⭐⭐(简单) | — | ⭐⭐ | ⭐⭐ |
| 身份证/手机号 | ⭐⭐⭐ | — | ⭐⭐ | — |
| 加解密 | ⭐⭐ | — | ⭐ | — |
| 文件操作 | ⭐⭐⭐ | ⭐ | ⭐⭐⭐ | ⭐⭐ |
| JSON | ⭐⭐ | — | — | — |
| 集合操作 | ⭐⭐ | ⭐⭐⭐ | ⭐⭐ | ⭐⭐ |
LLM最常犯的错误
| # | 错误 | 正确做法 |
|---|
| 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 |
核心规则速查
StrUtil.isBlank(null);
StrUtil.isBlank("");
StrUtil.isBlank(" ");
StrUtil.isEmpty(" ");
StrUtil.format("Hello {}, 欢迎来到{}!", "张三", "上海");
StrUtil.format("你好, {name}, 欢迎来到{city}!",
MapUtil.of("name", "张三", "city", "上海"));
String content = StrUtil.subBetween("prefix_content_suffix", "prefix_", "_suffix");
StrUtil.removePrefix("prefix_content", "prefix_");
StrUtil.removeSuffix("content_suffix", "_suffix");
List<String> parts = StrUtil.split("a,b, c ,d", ',', 0, true, true);
DateUtil.parse("2025-08-04");
DateUtil.parse("2025/08/04 10:30:00");
DateUtil.parse("2025年08月04日");
DateUtil.offsetDay(new Date(), 7);
DateUtil.between(begin, end, DateUnit.DAY);
DateUtil.beginOfMonth(new Date());
DateUtil.endOfMonth(new Date());
String result = HttpUtil.get("https://api.example.com/data");
HttpUtil.post(url, params);
HttpUtil.post(url, jsonBody);
String result = HttpUtil.createPost(url)
.header("Authorization", "Bearer xxx")
.header("Content-Type", "application/json")
.timeout(5000)
.body(jsonBody)
.execute()
.body();
IdcardUtil.isValidCard("440101199001011234");
int age = IdcardUtil.getAgeByIdCard(idCard);
String gender = IdcardUtil.getGenderByIdCard(idCard);
String birth = IdcardUtil.getBirthByIdCard(idCard);
String province = IdcardUtil.getProvinceByIdCard(idCard);
DesensitizedUtil.idCardNum(idCard, 1, 2);
DesensitizedUtil.mobilePhone("13800138000");
DesensitizedUtil.email("test@example.com");
Validator.isMobile("13800138000");
Validator.isEmail("test@example.com");
Validator.isCitizenId("440101199001011234");
CollUtil.isEmpty(list);
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: 身份证校验三段式
if (Validator.isCitizenId(idCard)) {
if (IdcardUtil.isValidCard(idCard)) {
int age = IdcardUtil.getAgeByIdCard(idCard);
String gender = IdcardUtil.getGenderByIdCard(idCard);
}
}
模式 2: 缓存空值防护
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";
}
return result;
});
}
模式 3: 多条件字符串处理链
String clean = StrUtil.trim(input);
String named = StrUtil.toCamelCase(clean);
List<String> parts = StrUtil.split(named, ',');
Gotchas
- isBlank vs isEmpty — isEmpty 不识别空白字符串(空格/tab/换行)
- HttpUtil 默认无连接池 — 高并发场景用 OkHttp,HttpUtil 适合简单场景
- JSONUtil 日期格式默认 yyyy-MM-dd HH:mm:ss — 与 Jackson 不同
- BeanUtil.copyProperties 是浅拷贝 — 嵌套对象不拷贝(引用共享)
- SecureUtil.sha256() 返回 hex 字符串 — 不是 byte[]
- DateUtil.parse() 自动识别格式有时误判 — 明确格式用 parse(dateStr, "yyyy-MM-dd")
- IdcardUtil 仅支持中国大陆18/15位身份证 — 港澳台/外籍不支持
- Hutool 功能太多(500+类) — 不要全部引入,按需使用子模块
- DateUtil.parse() 线程安全 — 底层 ThreadLocal 持有 SimpleDateFormat
- CollUtil.groupBy() 按字段分组 — 替代手写 Map 循环
Data Privacy
本技能不收集、存储或传输任何用户数据。