JAVA | Hutool Super easy-to-use tool library ~ you deserve it

I visited GitHub before and found a super easy-to-use tool class. After introducing it into the project, I found it is too convenient. It encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, regularization, threads, and XML to form various Util tool class. Just import it in the POM file when using it.

Introduction

Hutool is a small but comprehensive Java tool class library. Through static method encapsulation, it reduces the learning cost of related APIs, improves work efficiency, makes Java as elegant as a functional language, and makes the Java language "sweet".

The tools and methods in Hutool come from each user's meticulous attention to detail. It covers all aspects of the underlying code of Java development. It is not only a sharp tool to solve small problems in large-scale project development, but also an efficiency responsibility in small-scale projects;

Hutool is a friendly replacement for the "util" package in the project. It saves developers the time of encapsulating public classes and public tool methods in the project, allowing development to focus on business while avoiding bugs caused by imperfect encapsulation to the greatest extent.

Function

A Java basic tool class that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, regularization, threads, and XML to form various Util tool classes, and provides the following components at the same time:

  • hutool-aop JDK dynamic proxy package, providing aspect support under non-IOC

  • hutool-bloomFilter Bloom filter, providing Bloom filter for some Hash algorithms

  • hutool-cache cache

  • hutool-core core, including Bean operations, dates, various Utils, etc.

  • hutool-cron timing task module, providing timing tasks like Crontab expressions

  • hutool-crypto encryption and decryption module

  • hutool-db JDBC encapsulated data operation, based on the idea of ​​ActiveRecord

  • hutool-dfa Multi-keyword search based on DFA model

  • hutool-extra extension module, for third-party packaging (template engine, mail, etc.)

  • hutool-http Http client encapsulation based on HttpUrlConnection

  • hutool-log is a log façade implemented by automatically identifying logs

  • hutool-script script execution package, such as Javascript

  • hutool-setting More powerful Setting configuration file and Properties package

  • hutool-system System parameter call encapsulation (JVM information, etc.)

  • hutool-json JSON implementation

  • hutool-captcha image verification code implementation

use

Introduce the POM file directly in the project

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

Core (Hutool-core)

Type conversion tool class - Convert

  • Pain points
    In Java development, we have to face various type conversion problems, especially user parameters obtained from the command line, Parameter obtained from HttpRequest, etc. These parameters are of various types, how do we convert them? The common method is to convert it into a String first, and then call the XXX.parseXXX method. You also have to bear the risk of conversion failure and have to add a layer of try catch. This small process will look very ugly and bloated if it is mixed in the business code.

  • Convert class
    The Convert class can be said to be a tool method class, which encapsulates conversions for common Java types and is used to simplify type conversions. Most of the methods in the Convert class are toXXX, and the parameters are Object, which can convert any possible type into a specified type. At the same time, the second parameter defaultValue is supported to return a default value when the conversion fails.

  • Java common type conversion

    1. 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);Copy to clipboardErrorCopied
    
    1. Convert to an array of the specified type:
    String[] b = {
          
           "1", "2", "3", "4" };
    //结果为Integer数组
    Integer[] intArray = Convert.toIntArray(b);
    
    long[] c = {
          
          1,2,3,4,5};
    //结果为Integer数组
    Integer[] intArray2 = Convert.toIntArray(c);Copy to clipboardErrorCopied
    
    1. Convert to date object:
    String a = "2017-05-06";
    Date value = Convert.toDate(a);Copy to clipboardErrorCopied
    
    1. convert to collection
    Object[] a = {
          
          "a", "你", "好", "", 1};
    List<?> list = Convert.convert(List.class, a);
    //从4.1.11开始可以这么用
    List<?> list = Convert.toList(a);
    

Time tool class-DateUtil

  • Origin

    Considering that Java itself has limited support for date and time, and the coexistence of Date and Calendar objects leads to confusion and complexity in the use of various methods, this tool class is used for encapsulation. The encapsulation is mainly the conversion between date and string, as well as providing positioning of the date (a month ago, etc.).

    For the Date object, for the sake of convenience, a DateTime class is used instead, inherited from the Date object, the main convenience is that the toString() method is overridden, and a string in the form of yyyy-MM-dd HH:mm:ss is returned, which is convenient Calling during output (such as logging, etc.) provides many convenient methods to operate on date objects, and DateTime will be introduced in related chapters.

  1. Mutual conversion between Date, long and Calendar
//当前时间
Date date = DateUtil.date();
//当前时间
Date date2 = DateUtil.date(Calendar.getInstance());
//当前时间
Date date3 = DateUtil.date(System.currentTimeMillis());
//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
String now = DateUtil.now();
//当前日期字符串,格式:yyyy-MM-dd
String today= DateUtil.today();
  1. formatted date output
String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);

//结果 2017/03/01
String format = DateUtil.format(date, "yyyy/MM/dd");

//常用格式的格式化,结果:2017-03-01
String formatDate = DateUtil.formatDate(date);

//结果:2017-03-01 00:00:00
String formatDateTime = DateUtil.formatDateTime(date);

//结果:00:00:00
String formatTime = DateUtil.formatTime(date);
  1. date time difference

Sometimes we need to calculate the time difference between two dates (difference in days, difference in hours, etc.), Hutool encapsulates such methods as the between method:

String dateStr1 = "2017-03-01 22:33:23";
Date date1 = DateUtil.parse(dateStr1);

String dateStr2 = "2017-04-01 23:33:23";
Date date2 = DateUtil.parse(dateStr2);

//相差一个月,31天
long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
  1. timer

Timers are used to count how long a piece of code or procedure takes

TimeInterval timer = DateUtil.timer();

//---------------------------------
//-------这是执行过程
//---------------------------------

timer.interval();//花费毫秒数
timer.intervalRestart();//返回花费时间,并重置开始时间
timer.intervalMinute();//花费分钟数
  1. other
//年龄
DateUtil.ageOfNow("1990-01-30");

//是否闰年
DateUtil.isLeapYear(2017);

Summarize

A few tool classes are briefly introduced. There are too many easy-to-use tool classes in HuTool, so I won’t give examples one by one. If there is a tool class that you don’t know how to use, you can download its source code and analyze it. ~~

Guess you like

Origin blog.csdn.net/u012294515/article/details/106563449