What is Hutool

Liverpoolfc.tv: hutool. Cn /

  • Hutool is a small and complete Java tool class library, through the static method encapsulation, reduce the related API learning cost, improve work efficiency, make Java has the elegant functional language, let the Java language can also “sweet”;

  • The tools and methods in Hutool come from the elaboration of each user. It covers all aspects of the Java development code. It is not only a sharp tool to solve small problems in large project development, but also an efficiency responsibility in small projects.

  • Hutool is a friendly alternative to the util package in a project. It saves developers the time to encapsulate common classes and common tool methods in a project, enables development to focus on the business, and avoids bugs caused by incomplete encapsulation to the maximum.

Getting started and Installing

Maven

Add the following to the project’s PM.xml dependencies:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.58.</version>
</dependency>
Copy the code

Gradle

compile 'cn. Hutool: hutool -all: 5.5.8'
Copy the code

The Maven project

Click one of the following links to download hutool -all-x.x.x. jar:

  • Maven Central Library 1
  • Maven Central Library 2

Note that Hutool 5.x supports JDK8+. There is no test for The Android platform and no guarantee that all tool classes or tool methods are available. If your project uses JDK7, use Hutool version 4.x

Compile the installation

Visit Hutool’s Gitee home page at gitee.com/loolly/huto… Download the entire project source code (v5-master or V5-dev branch) and go to the Hutool project directory to execute:

./hutool.sh installCopy to clipboardErrorCopied
Copy the code

You can then import using Maven.

Contains the components

Util utility class a Java basic utility class that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, re, threads, and XML. Util utility classes provide the following components:

The module introduce
hutool-aop JDK dynamic proxy encapsulation, providing non-IOC aspect support
hutool-bloomFilter Bloom filtering, which provides some Hash algorithms for Bloom filtering
hutool-cache Simple cache implementation
hutool-core Core, including Bean operations, dates, various utils, and so on
hutool-cron Scheduled task module that provides scheduled tasks like Crontab expressions
hutool-crypto Encryption and decryption module, providing symmetric, asymmetric and digest algorithm encapsulation
hutool-db JDBC encapsulated data manipulation, based on ActiveRecord ideas
hutool-dfa Multi-keyword search based on DFA model
hutool-extra Extension module, encapsulation for third parties (template engine, mail, Servlet, TWO-DIMENSIONAL code, Emoji, FTP, word segmentation, etc.)
hutool-http Http client encapsulation based on HttpUrlConnection
hutool-log Automatic identification of log facade of log implementation
hutool-script Scripts perform encapsulation, such as Javascript
hutool-setting More powerful Setting profile and Properties wrapper
hutool-system System parameters call encapsulation (JVM information, etc.)
hutool-json JSON implementation
hutool-captcha Image verification code implementation
hutool-poi Excel and Word encapsulation for POI
hutool-socket NIO and AIO Socket encapsulation based on Java

Each module can be imported individually as required, or all modules can be imported by hutool-all.

Date time tool -DateUtil

conversion

Conversion between Date, Long, and Calendar

// The current time
Date date = DateUtil.date();
// The current time
Date date2 = DateUtil.date(Calendar.getInstance());
// The current time
Date date3 = DateUtil.date(System.currentTimeMillis());
// Current time The value is a character string in the format of YYYY-MM-DD HH: MM :ss
String now = DateUtil.now();
// The current date is a character string in the format of YYYY-MM-dd
String today= DateUtil.today();
Copy the code

String transfer date

The dateutil.parse method automatically recognizes some common formats, including:

  • yyyy-MM-dd HH:mm:ss

  • yyyy/MM/dd HH:mm:ss

  • yyyy.MM.dd HH:mm:ss

  • Yyyy Year MM month DD day HH MM minute SS second

  • yyyy-MM-dd

  • yyyy/MM/dd

  • yyyy.MM.dd

  • HH:mm:ss

  • HH Indicates the time. Mm indicates the minute. Ss indicates the second

  • yyyy-MM-dd HH:mm

  • yyyy-MM-dd HH:mm:ss.SSS

  • yyyyMMddHHmmss

  • yyyyMMddHHmmssSSS

  • yyyyMMdd

  • EEE, dd MMM yyyy HH:mm:ss z

  • EEE MMM dd HH:mm:ss zzz yyyy

  • yyyy-MM-dd’T’HH:mm:ss’Z’

  • yyyy-MM-dd’T’HH:mm:ss.SSS’Z’

  • yyyy-MM-dd’T’HH:mm:ssZ

  • yyyy-MM-dd’T’HH:mm:ss.SSSZ

String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);
Copy the code

We can also use custom date format conversion:

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

Format date output

String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);

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

// Common formatting, result: 2017-03-01
String formatDate = DateUtil.formatDate(date);

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

// Result: 00:00:00
String formatTime = DateUtil.formatTime(date);
Copy the code

Gets a part of the Date object

Date date = DateUtil.date();
// Get the year portion
DateUtil.year(date);
// Get the months, counting from 0
DateUtil.month(date);
// Get the month enumeration
DateUtil.monthEnum(date);
/ /...
Copy the code

Start and end times

Sometimes we need to get the start and end times of each day, the start and end times of each month, etc. DateUtil also provides methods to do this:

String dateStr = "The 2017-03-01 22:33:23";
Date date = DateUtil.parse(dateStr);

// Start the day, result: 2017-03-01 00:00:00
Date beginOfDay = DateUtil.beginOfDay(date);

// End of day, result: 2017-03-01 23:59:59
Date endOfDay = DateUtil.endOfDay(date);
Copy the code

Date time offset

A date or time offset pointer that adds or subtracts minutes, hours, days, etc., from a date for the purpose of changing the date. Hutool also does a lot of packaging for it

String dateStr = "The 2017-03-01 22:33:23";
Date date = DateUtil.parse(dateStr);

// Results: 2017-03-03 22:33:23
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);

// Common offset, result: 2017-03-04 22:33:23
DateTime newDate2 = DateUtil.offsetDay(date, 3);

// Common offset, result: 2017-03-01 19:33:23
DateTime newDate3 = DateUtil.offsetHour(date, -3); Copy to clipboardErrorCopiedCopy the code

Simplified offset methods are provided for the current time (for example, yesterday, last week, last month, etc.) :

/ / yesterday.
DateUtil.yesterday()
/ / tomorrow
DateUtil.tomorrow()
/ / last week
DateUtil.lastWeek()
/ / next week
DateUtil.nextWeek()
/ / last month
DateUtil.lastMonth()
/ / next month
DateUtil.nextMonth()Copy to clipboardErrorCopied
Copy the code

Date time difference

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

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

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

// The difference is one month, 31 days
longbetweenDay = DateUtil.between(date1, date2, DateUnit.DAY); Copy to clipboardErrorCopiedCopy the code

Formatting time difference

Sometimes we want to see a readable time difference, such as XX days XX hours XX minutes XX seconds, using the dateutil. formatBetween method:

// level. MINUTE Specifies the exact MINUTE
String formatBetween = DateUtil.formatBetween(between, Level.MINUTE);
// Output: 31 days 1 hourConsole.log(formatBetween); Copy to clipboardErrorCopiedCopy the code

Horoscopes and animal signs

// "Capricorn"
String zodiac = DateUtil.getZodiac(Month.JANUARY.getValue(), 19);

/ / "dog"
String chineseZodiac = DateUtil.getChineseZodiac(1994); Copy to clipboardErrorCopiedCopy the code

other

/ / age
DateUtil.ageOfNow("1990-01-30");

// Whether leap year
DateUtil.isLeapYear(2017); Copy to clipboardErrorCopiedCopy the code

The last

All the above are copied from the official website, the official website has all the API are very detailed, you can go to learn…