京寵展信息指南
1386
2022-05-30
在我們開發(fā)Java項目的過程中,JDK的有些功能并沒有,經(jīng)常會有一些特殊的內(nèi)容要處理,比如:日期和時間處理,字符串、數(shù)字處理,生成二維碼等功能,如果自已一個一個寫不僅浪費時間,還要考慮效率可靠性。如果有現(xiàn)成一個成熟的工具類庫,那我們使用它何樂而不為呢,還能提高效率節(jié)省時間。
hutool簡介
Hutool是一個小而全的Java工具類庫,通過靜態(tài)方法封裝,它幫助我們簡化每一行代碼,減少每一個方法,將一些通用類和方法提煉出來,在項目中引用 jar 包后,促使我們拿來就用,從而提高工作效率。
模塊功能
它是一個 Java 基礎(chǔ)工具類,對文件、流、加密解密、轉(zhuǎn)碼、正則、線程、XML 等 JDK 方法進(jìn)行封裝,組成各種 Util 工具類,同時提供以下組件:
hutool-aop: JDK動態(tài)代理封裝,提供非IOC下的切面支持。
hutool-bloomFilter: 布隆過濾,提供一些Hash算法的布隆過濾。
hutool-cache: 簡單緩存實現(xiàn)。
hutool-core: 核心,包括Bean操作,日期,各種Util等等。
hutool-cron: 定時任務(wù)模塊,提供類Crontab表達(dá)式的定時任務(wù)。
hutool-crypto: 加密解密模塊,提供對稱,非對稱和摘要算法封裝。
hutool-dfa: 基于DFA模型的多關(guān)鍵字查找。
hutool-db: JDBC封裝后的數(shù)據(jù)操作,基于ActiveRecord思想。
hutool-extra: 擴(kuò)展模塊,對第三方封裝(模板引擎,郵件,Servlet,二維碼,Emoji,F(xiàn)TP,分詞等)。
hutool-http: 基于HttpUrlConnection的Http客戶端封裝。
hutool-log: 自動識別日志實現(xiàn)的日志門面。
hutool-script: 腳本執(zhí)行封裝,如:JavaScript。
hutool-setting: 功能更強(qiáng)大的Setting配置文件和Properties封裝。
hutool-system: 系統(tǒng)參數(shù)調(diào)用封裝(JVM信息等)。
hutool-json: JSON實現(xiàn)。
hutool-captcha: 圖片驗證碼實現(xiàn)。
hutool-poi: 針對POI中Excel和Word的封裝。
hutool-socket: 基于Java的NIO和AIO的Socket封裝。
hutool-jwt:JSON Web Token (JWT)封裝實現(xiàn)。
我們主要常用:hutool-core,hutool-crypto,hutool-extra,hutool-http,hutool-captcha
安裝方式
1.Maven
在項目的 pom.xml 的 dependencies 中加入以下內(nèi)容
2.Gradle
implementation 'cn.hutool:hutool-all:5.7.15'
3.引用 jar 包
-:https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.7.15/
項目工程右鍵->Build Path(構(gòu)建路徑)->Add External Archives...(添加外部歸檔),選中下載的“hutool-all-5.7.15.jar”文件。
使用hutool-core
通過引入 hutool-all 方式引入所有模塊,也可以根據(jù)需求單獨引入使用的模塊。
日期和時間
引入:import cn.hutool.core.date.DateUtil;
public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 獲取當(dāng)前時間 Date date = DateUtil.date(); System.out.println(date); // 當(dāng)前時間,格式:yyyy-MM-dd HH:mm:ss String now = DateUtil.now(); System.out.println(now); // 當(dāng)前日期,格式:yyyy-MM-dd String today= DateUtil.today(); System.out.println(today); // 字符串轉(zhuǎn)日期類型 String dateStr = "2021-11-06"; Date date1 = DateUtil.parse(dateStr); System.out.println(date1); } }
字符串
引入:import cn.hutool.core.util.StrUtil;
public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 字符串處理 String str = "hello world"; // hasEmpty只判斷是否為null或者空字符串,返回 true/false System.out.println(StrUtil.hasEmpty(str)); // 截取字符串,sub(字符串,起始位置索引,結(jié)束位置索引) String strSub1 = StrUtil.sub(str, 1, 3); System.out.println(strSub1); // 字符串模板,格式化 String str1 = "hello {}"; String str2 = StrUtil.format(str1, "world"); System.out.println(str2); } }
類型轉(zhuǎn)換
引入:import cn.hutool.core.convert.Convert;
public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 類型轉(zhuǎn)換 // 數(shù)字轉(zhuǎn)換字符串 int a = 1; String aStr = Convert.toStr(a); // 轉(zhuǎn)換為日期對象 String b = "2017-05-06"; Date value = Convert.toDate(b); // 轉(zhuǎn)換為集合 Object[] c = {3, "1", "hello", ""}; List> list = Convert.toList(c); System.out.println(list); } }
使用hutool-captcha
圖形驗證碼
引入:import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub // 圖形驗證碼 // 定義圖形驗證碼的長和寬 LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100); // 圖形驗證碼寫出,可以寫出到文件,也可以寫出到流 lineCaptcha.write("d:/line.png"); // 輸出code System.out.println(lineCaptcha.getCode()); // 驗證圖形驗證碼的有效性,返回 boolean 值 System.out.println(lineCaptcha.verify("abcd")); } }
注:關(guān)于 Hutool API 更多的學(xué)習(xí)內(nèi)容,請參考官方文檔:https://hutool.cn/docs/#/
溫馨提示
文章內(nèi)容如果寫的存在問題歡迎留言指出,讓我們共同交流,共同探討,共同進(jìn)步~~~
文章如果對你有幫助,動動你的小手點個贊,鼓勵一下,給我前行的動力。
Java
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。