Introducing Hutool: A Comprehensive Java Utility Library with Installation and Code Examples
This article introduces the Hutool Java utility library, outlines its many modules and features, shows how to add it via Maven, and provides practical code examples for date, string, number, map, encryption, and captcha utilities to help developers avoid reinventing the wheel.
Hutool is a popular open‑source Java utility library that aggregates a wide range of common functions—such as file handling, streams, encryption, regular expressions, threading, XML processing, and more—into convenient utility classes, helping developers write cleaner and faster code.
Key modules include:
hutool-aop – JDK dynamic proxy wrapper for aspect‑oriented programming without IoC.
hutool-bloomFilter – Bloom filter implementation with various hash algorithms.
hutool-cache – Caching utilities.
hutool-core – Core utilities covering beans, dates, and general helpers.
hutool-cron – Cron‑style scheduled tasks.
hutool-crypto – Encryption and decryption utilities.
hutool-db – JDBC wrapper based on ActiveRecord concepts.
hutool-dfa – Multi‑keyword search using DFA.
hutool-extra – Extensions for third‑party tools (template engines, mail, etc.).
hutool-http – HTTP client built on HttpUrlConnection.
hutool-log – Automatic log‑framework detection.
hutool-script – Script execution (e.g., JavaScript).
hutool-setting – Advanced configuration file handling.
hutool-system – System and JVM information utilities.
hutool-json – JSON handling.
hutool-captcha – Image captcha generation.
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>Simple usage examples
DateUtil – common date and time operations:
// Convert between Date, long, Calendar
Date date = DateUtil.date();
date = DateUtil.date(Calendar.getInstance());
date = DateUtil.date(System.currentTimeMillis());
String dateStr = "2017-03-01";
date = DateUtil.parse(dateStr);
date = DateUtil.parse(dateStr, "yyyy-MM-dd");
String format = DateUtil.format(date, "yyyy-MM-dd");
int year = DateUtil.year(date);
int month = DateUtil.month(date);
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 manipulation:
// Check emptiness
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);MapUtil – map creation and 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 – generating line captcha images:
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();
}By leveraging Hutool, developers can significantly improve productivity and reduce boilerplate code across Java projects.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.