Boost Java Development Efficiency with Hutool: Essential Utilities Explained

This article introduces the Hutool Java utility library, detailing its design principles and core features such as Convert, DateUtil, StrUtil, ReflectUtil, IdUtil, RandomUtil, BeanUtil, and JSONUtil, with code examples showing how each utility simplifies common development tasks and improves productivity.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Boost Java Development Efficiency with Hutool: Essential Utilities Explained

Preface

Today I introduce an open‑source toolkit that can help improve development efficiency: hutool.

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

The design philosophy aims to minimize repeated definitions, keeping the util package small. The main ideas are:

Method‑first over object‑first

Automatic detection over user definition

Convenience and flexibility coexist

Adaptability and compatibility

Optional dependencies

Non‑intrusive

Hutool provides utilities for files, streams, encryption, conversion, regex, threading, XML, etc., packaged into various Util classes to boost productivity.

Hutool overview
Hutool overview

To use Hutool, add the dependency to your pom.xml:

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

1. Convert

Hutool’s Convert class simplifies type conversions such as numbers to strings, arrays, collections, and dates.

Number to string:

int a = 1;
// aStr = "1"
String aStr = Convert.toStr(a);

Array to string:

long[] b = {1,2,3,4,5};
// bStr = "[1, 2, 3, 4, 5]"
String bStr = Convert.toStr(b);

String array to int array:

String[] b = {"1","2","3","4"};
Integer[] intArray = Convert.toIntArray(b);

Date conversion:

String a = "2017-05-06";
Date value = Convert.toDate(a);

Collection conversion:

Object[] a = {"a","你","好","",1};
List<?> list = Convert.toList(a);

2. DateUtil

Java’s built‑in date handling is limited; Hutool’s DateUtil offers convenient parsing, formatting, and start/end calculations.

2.1 Date and Calendar conversion

// current time
Date date = DateUtil.date();
Date date2 = DateUtil.date(Calendar.getInstance());

2.2 String to Date

String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);
Date date2 = DateUtil.parse(dateStr, "yyyy-MM-dd");

2.3 Formatting

String format = DateUtil.format(date, "yyyy/MM/dd");
String formatDate = DateUtil.formatDate(date);
String formatDateTime = DateUtil.formatDateTime(date);
String formatTime = DateUtil.formatTime(date);

2.4 Start and end of day

Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);

3. StrUtil

Similar to Apache Commons Lang’s StringUtils, StrUtil provides methods like isBlank, removePrefix, sub, and format.

3.1 hasBlank

Returns true if any given string is blank (including invisible characters).

3.2 removePrefix / removeSuffix

String fileName = StrUtil.removeSuffix("pretty_girl.jpg", ".jpg"); // "pretty_girl"

3.3 sub

String str = "abcdefgh";
String sub1 = StrUtil.sub(str, 2, 3); // "c"
String sub2 = StrUtil.sub(str, 2, -3); // "cde"

3.4 format

String template = "{}爱{},就像老鼠爱大米";
String result = StrUtil.format(template, "我", "你"); // "我爱你,就像老鼠爱大米"

4. ReflectUtil

Hutool wraps Java reflection to obtain constructors, fields, methods, and invoke them dynamically.

4.1 Get all methods

Method[] methods = ReflectUtil.getMethods(ExamInfoDict.class);

4.2 Get specific method

Method method = ReflectUtil.getMethod(ExamInfoDict.class, "getId");

4.3 Create instance

Object obj = ReflectUtil.newInstance(ExamInfoDict.class);

4.4 Invoke method

TestClass test = new TestClass();
ReflectUtil.invoke(test, "setA", 10);

5. IdUtil

Provides generators for UUID, ObjectId, and Snowflake IDs.

5.1 UUID

String uuid = IdUtil.randomUUID(); // with hyphens
String simpleUUID = IdUtil.simpleUUID(); // without hyphens

5.2 ObjectId

String id = ObjectId.next();
String id2 = IdUtil.objectId();

5.3 Snowflake

Snowflake snowflake = IdUtil.getSnowflake(1, 1);
long id = snowflake.nextId();
long id2 = IdUtil.getSnowflakeNextId();
String idStr = snowflake.getSnowflakeNextIdStr();

6. RandomUtil

Wraps JDK Random to generate integers, byte arrays, and random elements.

int c = RandomUtil.randomInt(10, 100);
byte[] bytes = RandomUtil.randomBytes(10);
Set<Integer> set = RandomUtil.randomEleSet(CollUtil.newArrayList(1,2,3,4,5,6), 2);

7. BeanUtil

Utilities for bean detection, bean‑to‑map conversion, and property copying.

boolean isBean = BeanUtil.isBean(HashMap.class);
Map<String,Object> map = BeanUtil.beanToMap(person);
BeanUtil.copyProperties(sourceBean, targetMap);

8. JSONUtil

Convenient methods for JSON creation, pretty printing, parsing, and conversion between JSON and XML.

SortedMap<Object,Object> sortedMap = new TreeMap<>();
sortedMap.put("attributes","a");
sortedMap.put("b","b");
sortedMap.put("c","c");
String json = JSONUtil.toJsonStr(sortedMap);
String pretty = JSONUtil.toJsonPrettyStr(sortedMap);
JSONObject obj = JSONUtil.parseObj(json);
String xml = JSONUtil.toXmlStr(JSONUtil.createObj().set("aaa","你好"));

Overall, Hutool’s comprehensive utilities can significantly reduce boilerplate code and boost Java development productivity.

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 DevelopmenthutoolCode ExamplesUtility Library
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

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.