Boost Java Projects with Hutool: Essential Utility Library Overview

This article introduces Hutool, a comprehensive Java utility library that consolidates common functions such as file handling, encryption, date manipulation, and more, explains its key modules, shows how to add it via Maven, and provides practical code examples for utilities like DateUtil, StrUtil, NumberUtil, and SecureUtil.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Boost Java Projects with Hutool: Essential Utility Library Overview

Today we recommend a very useful Java utility library, Hutool, which provides many enterprise‑level common utilities, helping avoid reinventing the wheel and saving development time.

Hutool (pronounced “hu‑tu”) aims for a carefree approach to coding.

Hutool is a Java toolkit that simplifies code by offering a wide range of utilities such as file I/O, streams, encryption/decryption, encoding, regular expressions, threading, XML handling, and more.

Official website: https://www.hutool.cn/

1. Features

Hutool wraps many JDK methods for files, streams, encryption, encoding, regex, threading, XML, etc., and provides the following components:

tool-aop – JDK dynamic proxy wrapper, offering AOP support without IoC.

hutool-bloomFilter – Bloom filter with various hash algorithms.

hutool-cache – Caching utilities.

hutool-core – Core utilities including bean operations, dates, 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 based on DFA model.

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

hutool-http – Http client built on HttpUrlConnection.

hutool-log – Log facade that auto‑detects the underlying implementation.

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

hutool-setting – Advanced configuration file and Properties handling.

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

hutool-json – JSON utilities.

hutool-captcha – Image captcha implementation.

2. Installation

Add the following dependency to your Maven pom.xml:

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

3. Simple Tests

DateUtil – Date and time utilities.

// Convert between Date, long, Calendar
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 parsing
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);
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.

// 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 = "This is a placeholder:{}";
String result = StrUtil.format(template, "I am the placeholder");
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 conversion and copying.

PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName("Xiaomi");
brand.setShowStatus(0);
Map<String, Object> 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);
BeanUtil.copyProperties(brand, copyBrand);
LOGGER.info("beanUtil copy properties:{}", copyBrand);

MapUtil – Map creation and checks.

Map<Object, Object> 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 image captcha.

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 Hutool utilities, refer to the official site: https://www.hutool.cn/
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentmavenHutoolCode examplesutility library
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

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.