Backend Development 8 min read

Introduction to Hutool: A Comprehensive Java Utility Library

This article introduces Hutool, a versatile Java utility library that consolidates common functions such as file handling, encryption, caching, and more, provides Maven installation instructions, and demonstrates practical code examples for date, string, number, map, security, and captcha utilities.

Architecture Digest
Architecture Digest
Architecture Digest
Introduction to Hutool: A Comprehensive Java Utility Library

Hutool is a Java utility library that aggregates frequently used tools to simplify development, reduce repetitive code, and improve efficiency across various domains such as file I/O, encryption, caching, and more.

Features

tool-aop: JDK dynamic proxy wrapper for AOP without IoC.

hutool-bloomFilter: Bloom filter implementations with several hash algorithms.

hutool-cache: Caching utilities.

hutool-core: Core utilities including Bean operations, dates, and general 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 using DFA models.

hutool-extra: Extensions for third‑party integrations (template engines, mail, etc.).

hutool-http: Http client built on HttpUrlConnection.

hutool-log: Automatic log facade detection.

hutool-script: Script execution wrapper (e.g., JavaScript).

hutool-setting: Enhanced configuration file and Properties handling.

hutool-system: System parameter access (JVM info, etc.).

hutool-json: JSON utilities.

hutool-captcha: Image captcha implementation.

Installation

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.6.3</version>
</dependency>

Simple Tests

DateUtil – Demonstrates date conversion, formatting, and arithmetic.

//Date、long、Calendar之间的相互转换
//当前时间
Date date = DateUtil.date();
//Calendar转Date
date = DateUtil.date(Calendar.getInstance());
//时间戳转Date
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 – Shows string 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 str2 = StrUtil.format(template, "我是占位符");
LOGGER.info("/strUtil format:{}", str2);

NumberUtil – Provides arithmetic operations, rounding, and type checks.

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 – Illustrates map creation and emptiness checks.

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

SecureUtil – Demonstrates MD5 hashing.

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

CaptchaUtil – Shows how to generate and serve a 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();
}

For more utilities and detailed documentation, visit the official Hutool website at https://www.hutool.cn/ and additional resources at https://www.yoodb.com/ . Using Hutool can significantly boost development productivity.

JavaBackend DevelopmentmavenHutoolcode examplesUtility Library
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.