Unlock Java Productivity: Master Hutool’s Powerful Utility Library

This article introduces the Hutool Java utility library, outlines its modules, shows how to add it to a Maven project, and provides practical code examples for HTTP requests, random codes, pinyin conversion, timing, number handling, data masking, email sending, and Bloom filters, demonstrating how Hutool can streamline backend development.

macrozheng
macrozheng
macrozheng
Unlock Java Productivity: Master Hutool’s Powerful Utility Library
Hutool logo
Hutool logo

In daily development we often use utility classes to speed up Java projects, and Hutool is a popular one in China.

Hutool is a small yet comprehensive Java utility library that encapsulates APIs via static methods, reducing learning cost and making Java feel as elegant as functional languages.

It covers many aspects of low‑level Java code and serves both large projects for solving small problems and small projects for efficiency.

1. Modules Included

Hutool provides the following modules:

hutool-aop : JDK dynamic proxy encapsulation, provides AOP support without IoC.

hutool-bloomFilter : Bloom filter implementation with various hash algorithms.

hutool-cache : Simple cache implementation.

hutool-core : Core utilities including bean operations, date handling, and common utils.

hutool-cron : Scheduling module supporting Crontab expressions.

hutool-crypto : Encryption and decryption utilities for symmetric, asymmetric, and digest algorithms.

hutool-db : JDBC wrapper based on ActiveRecord concepts.

hutool-dfa : Multi‑keyword search based on DFA model.

hutool-extra : Extensions for third‑party integrations such as template engines, mail, servlet, QR code, Emoji, FTP, and word segmentation.

hutool-http : Http client wrapper based on HttpUrlConnection.

hutool-log : Automatic log implementation façade.

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.

hutool-poi : POI wrappers for Excel and Word.

hutool-socket : Socket wrapper based on Java NIO/AIO.

hutool-jwt : JSON Web Token (JWT) implementation.

2. Adding the Framework

Add the following dependency to pom.xml:

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

3. Common Use Cases

3.1 HTTP Request

Hutool‑http wraps HttpURLConnection, simplifying HTTPS requests, file upload, and cookie handling.

GET request example:

// GET request
String content = HttpUtil.get(url);

POST request example:

// POST request
HashMap<String, Object> param = new HashMap<>();
param.put("city", "Xi'an");
String result = HttpUtil.post("www.javacn.site", param);

3.2 Generating Random Verification Codes

// Generate a 4‑digit random verification code
String verificationCode = RandomUtil.randomStringUpper(4);

3.3 Pinyin Utilities

Hutool provides a façade for several pinyin libraries (TinyPinyin, JPinyin, Pinyin4j) and automatically selects the available one.

Example to get full pinyin:

// Output: ni hao
String pinyin = PinyinUtil.getPinyin("你好", " ");

Example to get first letters:

// Output: h, s, d, y, g
String result = PinyinUtil.getFirstLetter("H是第一个", ", ");

3.4 Timer

TimeInterval

offers simple timing of code execution.

TimeInterval timer = DateUtil.timer();
// ... business logic
long ms = timer.interval(); // elapsed milliseconds
timer.intervalRestart(); // get elapsed time and reset

3.5 Number Utilities

NumberUtil

provides rounding, formatting, validation, and random number generation.

double te1 = 123456.123456;
double te2 = 123456.128456;
Console.log(round(te1, 4)); // 123456.1235
Console.log(round(te2, 4)); // 123456.1285

Formatting example:

long c = 299792458;
String format = NumberUtil.decimalFormat(",###", c); // 299,792,458

3.6 Data Desensitization

Hutool offers utilities to mask sensitive data such as ID cards, phone numbers, passwords, etc.

// Mask ID card number, keep first and last two characters
DesensitizedUtil.idCardNum("51343620000320711X", 1, 2);
// Mask phone number
DesensitizedUtil.mobilePhone("18049531999");

3.7 Email Sending

MailUtil

wraps JavaMail for simple email sending.

// Send a plain‑text email
MailUtil.send("[email protected]", "Test", "Email from Hutool", false);

3.8 Bloom Filter

Hutool includes a Bloom filter implementation for fast set membership tests.

// Initialize and use a Bloom filter
BitMapBloomFilter filter = new BitMapBloomFilter(10);
filter.add("123");
filter.add("abc");
bool exists = filter.contains("abc");

Conclusion

Hutool is a cost‑effective utility library that can make Java code more concise and pleasant to write.

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.

javahutoolCode ExamplesUtility Library
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.