Introducing Hutool: A Comprehensive Java Utility Library and Its Usage
This article introduces the Hutool Java utility library, outlines its extensive modules such as AOP, cache, cron, crypto, and provides installation instructions and code examples for common utilities like DateUtil, StrUtil, NumberUtil, MapUtil, SecureUtil, and CaptchaUtil to boost development efficiency.
The author, Ryan Wang, recommends the Hutool Java utility library, a comprehensive collection of tools that simplify everyday coding tasks, reduce boilerplate, and help developers avoid reinventing the wheel.
Key Modules
hutool-aop – JDK dynamic proxy wrapper providing AOP support without IoC.
hutool-bloomFilter – Bloom filter implementation with various hash algorithms.
hutool-cache – Caching utilities.
hutool-core – Core utilities including Bean operations, date handling, and common helpers.
hutool-cron – Cron-like scheduled task module.
hutool-crypto – Encryption and decryption utilities.
hutool-db – JDBC wrapper based on ActiveRecord concepts.
hutool-dfa – Multi‑keyword search based on DFA model.
hutool-extra – Extensions for third‑party integrations (template engines, mail, etc.).
hutool-http – Http client built on HttpUrlConnection.
hutool-log – Automatic detection of logging implementations.
hutool-script – Script execution wrapper (e.g., JavaScript).
hutool-setting – Advanced configuration file and Properties handling.
hutool-system – System parameter utilities (JVM info, etc.).
hutool-json – JSON handling utilities.
hutool-captcha – Image captcha generation utilities.
Installation
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>Simple Usage Examples
DateUtil – common date‑time operations:
// Date, long, Calendar conversion
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 utilities:
String str = "test";
StrUtil.isEmpty(str);
StrUtil.isNotEmpty(str);
StrUtil.removeSuffix("a.jpg", ".jpg");
StrUtil.removePrefix("a.jpg", "a.");
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);
NumberUtil.isNumber("1.234");
NumberUtil.isInteger("123");
NumberUtil.isDouble("1.234");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 image captchas:
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();
}Overall, Hutool provides a rich set of ready‑to‑use utilities that can greatly improve Java development productivity; readers are encouraged to visit the official website for more modules and examples.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.