Backend Development 8 min read

An Introduction to the Hutool Java Utility Library and Its Common Tools

This article introduces the Hutool Java utility library, outlines its extensive feature set, shows how to add it via Maven, and provides code examples for frequently used utilities such as DateUtil, StrUtil, NumberUtil, MapUtil, SecureUtil, and CaptchaUtil, helping developers boost backend productivity.

Top Architecture Tech Stack
Top Architecture Tech Stack
Top Architecture Tech Stack
An Introduction to the Hutool Java Utility Library and Its Common Tools

Hutool is a comprehensive Java utility library that simplifies everyday coding tasks by providing wrappers for JDK functionalities such as file handling, streams, encryption, encoding, regular expressions, threading, and XML processing.

Key modules include tool-aop for dynamic proxy AOP, hutool-bloomFilter for Bloom filter implementations, hutool-cache for caching, hutool-core for core utilities (Bean operations, dates, etc.), hutool-cron for cron-like scheduling, hutool-crypto for encryption/decryption, hutool-db for JDBC/ActiveRecord style data access, hutool-dfa for multi‑keyword search, hutool-extra for third‑party integrations, hutool-http for HTTP client, hutool-log for logging abstraction, hutool-script for script execution, hutool-setting for advanced configuration handling, hutool-system for JVM information, hutool-json for JSON processing, and hutool-captcha for image captchas.

To use Hutool in a Maven project, add the following dependency to pom.xml :

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>${hutool.version}</version>
</dependency>

Below are examples of several frequently used utility classes:

DateUtil

Provides date and time conversion, formatting, and calculation methods.

// 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

Offers common string operations such as emptiness checks, prefix/suffix removal, and formatting.

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

Handles arithmetic and validation for numeric types.

double n1 = 1.234;
double n2 = 1.234;
double 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.23");

MapUtil

Facilitates map creation and emptiness checks.

Map
map = MapUtil.of(new String[][]{
    {"key1", "value1"},
    {"key2", "value2"},
    {"key3", "value3"}
});
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);

SecureUtil

Provides simple cryptographic functions such as MD5 hashing.

String str = "123456";
String md5Str = SecureUtil.md5(str);
LOGGER.info("secureUtil md5:{}", md5Str);

CaptchaUtil

Generates image captchas for verification.

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();
}

For a full list of utilities and detailed documentation, visit the official Hutool website at https://www.hutool.cn/ . Using Hutool can significantly improve development efficiency for Java backend projects.

BackendJavaHutoolUtilitytoolsCodeExamples
Top Architecture Tech Stack
Written by

Top Architecture Tech Stack

Sharing Java and Python tech insights, with occasional practical development tool tips.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.