Introducing Hutool: A Comprehensive Java Utility Library
This article introduces Hutool, a Java utility library that consolidates common enterprise‑level tools such as file handling, encryption, date manipulation, and data conversion, explains its main modules, shows how to add it via Maven, and provides practical code examples for quick integration.
Hutool (pronounced “hu‑tu”) is a Java utility library that aggregates a wide range of common functions—file I/O, encryption/decryption, date handling, regular expressions, threading, XML processing, and more—into convenient utility classes, helping developers avoid reinventing the wheel and speeding up development.
1. Features
to‑aop: JDK dynamic proxy wrapper providing AOP support without an IoC container.
hutool‑bloomFilter: Bloom filter implementation with various hash algorithms.
hutool‑cache: Caching utilities.
hutool‑core: Core utilities including Bean operations, date handling, and general helpers.
hutool‑cron: Cron‑like scheduling module.
hutool‑crypto: Encryption and decryption utilities.
hutool‑db: JDBC wrapper based on ActiveRecord concepts.
hutool‑dfa: Multi‑keyword search using DFA model.
hutool‑extra: Extensions for third‑party integrations (template engines, mail, etc.).
hutool‑http: HttpUrlConnection‑based HTTP client.
hutool‑log: Automatic detection of logging implementations.
hutool‑script: Script execution wrapper (e.g., JavaScript).
hutool‑setting: Enhanced configuration file and Properties handling.
hutool‑system: System parameter utilities (JVM info, etc.).
hutool‑json: JSON processing utilities.
hutool‑captcha: Image captcha generation.
2. Installation
Add the following dependency to your Maven pom.xml :
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>3. Simple Tests
DateUtil – common date‑time operations:
// Date, long, Calendar conversion
Date date = DateUtil.date(); // current time
date = DateUtil.date(Calendar.getInstance()); // Calendar to Date
date = DateUtil.date(System.currentTimeMillis()); // timestamp to Date
String dateStr = "2017-03-01";
date = DateUtil.parse(dateStr); // auto format detection
date = DateUtil.parse(dateStr, "yyyy-MM-dd"); // custom format
String format = DateUtil.format(date, "yyyy-MM-dd");
int year = DateUtil.year(date);
int month = DateUtil.month(date); // 0‑based
Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);StrUtil – string utilities:
// Empty check
String str = "test";
StrUtil.isEmpty(str);
StrUtil.isNotEmpty(str);
// Remove suffix/prefix
StrUtil.removeSuffix("a.jpg", ".jpg");
StrUtil.removePrefix("a.jpg", "a.");
// Format string
String template = "这只是个占位符:{}";
String result = StrUtil.format(template, "我是占位符");
LOGGER.info("/strUtil format:{}", result);NumberUtil – numeric operations:
double n1 = 1.234;
double n2 = 1.234;
double result;
result = NumberUtil.add(n1, n2);
result = NumberUtil.sub(n1, n2);
result = NumberUtil.mul(n1, n2);
result = NumberUtil.div(n1, n2);
BigDecimal roundNum = NumberUtil.round(n1, 2);
String n3 = "1.234";
NumberUtil.isNumber(n3);
NumberUtil.isInteger(n3);
NumberUtil.isDouble(n3);BeanUtil – JavaBean ↔ Map conversion and property copying:
PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName("小米");
brand.setShowStatus(0);
Map
map = BeanUtil.beanToMap(brand);
LOGGER.info("beanUtil bean to map:{}", map);
PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
LOGGER.info("beanUtil map to bean:{}", mapBrand);
PmsBrand copyBrand = new PmsBrand();
BeanUtil.copyProperties(brand, copyBrand);
LOGGER.info("beanUtil copy properties:{}", copyBrand);MapUtil – map creation and emptiness checks:
Map
map = MapUtil.of(new String[][]{
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
});
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);SecureUtil – MD5 hashing:
String str = "123456";
String md5Str = SecureUtil.md5(str);
LOGGER.info("secureUtil md5:{}", md5Str);CaptchaUtil – generate line captcha image:
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
try {
request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
response.setContentType("image/png");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
lineCaptcha.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}Hutool provides many additional utilities; exploring its official website (https://www.hutool.cn/) can reveal further helpful methods that significantly improve development efficiency.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.