preface
Recently found a good tool class library Hutool, in the development of the use is very convenient!! This paper mainly introduces the Hutool class library to record the common external tool classes in Java development, using these tool classes can greatly improve the efficiency of development.
Hutool is a small and complete Java tool class library, through the static method encapsulation, reduce the cost of learning related API, improve work efficiency, is the project “util” package friendly replacement, it saves the developer on the project public class and public tool method encapsulation time, make the development focus on business. At the same time, it can avoid bugs caused by incomplete encapsulation to the maximum extent.
Depend on the introduction of
🍊 Maven
Add the following to the project’s PM.xml dependencies:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
Copy the code
🍐 Gradle
Implementation 'cn. Hutool: hutool -all: 5.7.15'Copy the code
component
The document
📙 reference API
📘 Chinese document
Common Tool Classes
Type conversion utility class -Convert
Universal type converters and implementation classes for various type conversions, where Convert is the converter entry and provides various toXXX methods and Convert methods
Int a = 1; String aStr = Convert.toStr(a); // Convert to the specified type array String[] b = {"1", "2", "3", "4"}; Integer[] bArr = Convert.toIntArray(b); // Convert to date object String dateStr = "2017-05-06"; Date date = Convert.toDate(dateStr); String[] strArr = {"a", "work "," with ", "d"}; List<String> strList = Convert.toList(String.class, strArr);Copy the code
Date time tool -DateUtil
The datetime utility class defines some common datetime manipulation methods. Date and Calendar objects in the JDK are really hard to use, so it’s much easier to manipulate dates and times!
Date = dateutil.date (); //Date = dateutil.date (); Date = dateutil. Date (calendar.getinstance ()); 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); BeginOfDay = dateutil. beginOfDay(Date); Date endOfDay = DateUtil.endOfDay(date); Date newDate = dateutil. offset(Date, datefield.day_of_month, 2); // Calculate the offset Date. Long betweenDay = dateutil. between(date, newDate, dateunit.day); long betweenDay = dateutil. between(date, newDate, dateunit.day); Long betweenDay = dateutil. between(date1, date2, dateunit.day); long betweenDay = dateutil. between(date1, date2, DateUnit.Copy the code
String tool -StrUtil
The string utility class defines some common string manipulation methods. StrUtil is shorter and easier to use than StringUtil!
String STR = "test"; StrUtil.isEmpty(str); StrUtil.isNotEmpty(str); String fileName = strutil. removeSuffix("test.jpg", ".jpg") String fileName = strutil. removePrefix("test.jpg", ".jpg") // Formatting String template = "Hello, I am {}"; String str2 = StrUtil.format(template, "test");Copy the code
File utility class -FileUtil
// create a new folder fileutil. mkdir("D:\"); // Create a new file fileutil. file("D:\", "F:\"); // Create a temporary file fileutil. createTempFile(fileutil. file("D:\")); Fileutil. isEmpty(fileutil. file("D:\")); FileUtil.isDirectory("D:\"); FileUtil.isFile("D:\");Copy the code
Array utility -ArrayUtil
Array tool class is mainly to solve the object array (including the wrapper type array) and primitive type array use method is not unified.
/ / found empty
int[] a = {};
int[] b = null;
ArrayUtil.isEmpty(a);
ArrayUtil.isEmpty(b);
// Create a new generic array
String[] newArray = ArrayUtil.newArray(String.class, 3);
// Generic array merge
int[] result = ArrayUtil.addAll(a, b);
/ / filter
Integer[] a = {1.2.3.4.5.6};
/ / minus [2]
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2= =0)? t :null);
// Whether to include
boolean flag = ArrayUtil.contains(a, 1);
// Convert an array to a string using a spacer
String result = ArrayUtil.join(new int[]{1.2.3.4}, "-");
Copy the code
Collection tool -CollUtil
The collection operations utility class defines some common collection operations.
/ / an array list is converted to a String [] array = new String [] {" a ", "b", "c", "d", "e"}; List<String> list = CollUtil.newArrayList(array); //join: String joinStr = CollUtil. Join (list, ","); LOGGER.info("collUtil join:{}", joinStr); List<String> splitList = strutil.split (joinStr, ','); LOGGER.info("collUtil split:{}", splitList); // create a newMap, Set, List HashMap<Object, Object> newMap = collutil.newhashmap (); HashSet<Object> newHashSet = CollUtil.newHashSet(); ArrayList<Object> newList = CollUtil.newArrayList(); // check whether the list isEmpty collutil. isEmpty(list);Copy the code
Number tools -NumberUtil
Number processing tool class, can be used for various types of numbers of addition, subtraction, multiplication and division operations and type judgment.
// Encapsulate methods in BigDecimal to preserve decimals double te1=123456.123456; Double te2 = 123456.128456; The Console. The log (round (te1, 4)); // Result :123456.1235 console. log(round(te2,4)); // result :123456.1285 // Add, subtract, multiply and divide float, double, BigDecimal result = numberUtil. add(te1, te2); result = NumberUtil.sub(te1, te2); result = NumberUtil.mul(te1, te2); result = NumberUtil.div(te1, te2); BigDecimal roundNum = NumberUtil. Round (te1, 2); String te3 = "1.234"; Numberutil. isNumber(te1); numberUtil. isNumber(te1); NumberUtil.isInteger(te1); NumberUtil.isDouble(te1); NumberUtil.isPrimes(te1)Copy the code
Field Validator -Validator
Field validator can validate strings in different formats, such as email, mobile phone number, IP, and so on.
// Check whether it is an email address
boolean result = Validator.isEmail("[email protected]");
// Check whether it is a mobile phone number
result = Validator.isMobile("18911111111");
// Check whether it is an IPV4 address
result = Validator.isIpv4("192.168.5.130");
// Check if it is Chinese
result = Validator.isChinese("Hello");
// Check whether it is id number (18 digits Chinese)
result = Validator.isCitizenId("123456");
// Check whether it is a URL
result = Validator.isUrl("http://www.baidu.com");
Copy the code
Other Tool classes
There are many tools in Hutool. If you are interested, you can check out the tutorials on the official website: www.hutool.cn/