This edition recommends a Java utility class library called Hutool.
Hutool is a small and complete Java utility class library, through static method encapsulation, reduce the learning cost of related apis, improve work efficiency, and make Java have a functional language like elegance. Hutool is a friendly alternative to the “util” package in a project. It saves developers time to package common classes and common tool methods in the project, keeps development focused on the business, and minimizes bugs caused by imperfect packaging.
Hutool includes:
rely on
- Maven
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.13</version></dependency>
- Gradle
implementation 'cn.hutool:hutool-all:5.7.13'
Example:
Type conversion utility class -Convert
The Convert class encapsulates conversions for common Java types to simplify type conversions.
/***Convert to string*/int a = 1;//aStr"1"String aStr = Convert.toStr(a);long[] b = {1,2,3,4,5};//bStr:"[1, 2, 3, 4, 5]"String bStr = Convert.toStr(b);
/*** Convert to set*/Object[] a = {"a", "you", "good", "", 1};List<?> list = Convert.convert(List.class, a);//form 4.1.11You can start with thatList<?> list = Convert.toList(a);
- Date and Time tool -DateUtil
DateUtil encapsulates mainly the conversion between date and string, and provides the location of the date (one month ago, etc.)
/*** Date、long、CalendarConversion between */// Current time Date date = DateUtil.date();//current timeDate date2 = DateUtil.date(Calendar.getInstance());//current timeDate date3 = DateUtil.date(System.currentTimeMillis());//Current date string, format:yyyy-MM-dd HH:mm:ssString now = DateUtil.now();//Current date string, format:yyyy-MM-ddString today= DateUtil.today();
- File utility class -FileUtil
The FileUtil class contains the following operation tools:
File operations: include creating, deleting, copying, moving, and changing names of file directories
File judgment: Determine whether the file or directory is non-empty, whether it is a directory, whether it is a file, and so on.
Absolute path: Convert files in the ClassPath to absolute path files.
File name: The name of the main file and the extension
Read operations: including getReader and readXXX operations in IoUtil
Write operations: include getWriter and writeXXX operations
Unique ID tool -IdUtil
/*** Generate UUID*/// The generated UUID is a string with -, similar to: a5c8a5e8-df2b-4706-bea4-08d0939410e3String uuid = IdUtil.randomUUID(); // Generates a string without -, similar to:b17f24ff026d40949c85a24f4f375d42String simpleUUID = IdUtil.simpleUUID();
/*** Generate Snowflake*/// Parameter 1 is the terminal ID// Parameter 2 is the data centerIDSnowflake snowflake = IdUtil.getSnowflake(1, 1);long id = snowflake.nextId();
- DesensitizedUtil Information desensiTIZEDUtil
// Id number 5 * * * * * * * * * * * * * * * 1 xdesensitizedutil. IdCardNum (" x 51343620000320711 ", 1, 2); // Mobile phone number180****1999DesensitizedUtil.mobilePhone("18049531999");
Read the Hutool reference for more information on your ownHutool参考文档。