這幾個常用的工具類,讓你生產力爆表!
前言
Hutool是一個優秀的Java工具類庫,提供了豐富的工具類和方法,能夠簡化Java開發過程中的許多常見任務。本文將介紹四個常用的Hutool工具類,并給出每個工具類的四個實際例子,幫助你更好地了解和使用Hutool。
大綱
圖片
StrUtil工具類
StrUtil工具類提供了一系列字符串處理的方法,讓字符串操作變得簡單和高效。
import cn.hutool.core.util.StrUtil;
public class StrUtilExample {
public static void main(String[] args) {
// 判斷字符串是否為空
String str = "";
boolean isEmpty = StrUtil.isEmpty(str);
System.out.println(isEmpty); // 輸出:true
// 字符串拼接
String[] strs = {"Hello", "Hutool"};
String result = StrUtil.join("-", strs);
System.out.println(result); // 輸出:Hello-Hutool
// 字符串截取
String str = "Hello, World!";
String result = StrUtil.sub(str, 7, 12);
System.out.println(result); // 輸出:World
// 字符串格式化
String name = "Alice";
int age = 25;
String result = StrUtil.format("My name is {}, and I'm {} years old.", name, age);
System.out.println(result); // 輸出:My name is Alice, and I'm 25 years old.
}
}
DateUtil工具類
DateUtil工具類提供了日期和時間處理的方法,方便地進行日期格式轉換、計算和比較。
import cn.hutool.core.date.DateUtil;
public class DateUtilExample {
public static void main(String[] args) {
// 獲取當前日期
Date now = DateUtil.date();
System.out.println(now); // 輸出:2023-01-01 10:30:00
// 日期格式化
String dateStr = "2023-01-01";
Date date = DateUtil.parse(dateStr);
String formattedDate = DateUtil.format(date, "yyyy/MM/dd");
System.out.println(formattedDate); // 輸出:2023/01/01
// 日期加減
Date date = DateUtil.date(); // 2023-01-01 10:30:00
Date nextWeek = DateUtil.offsetWeek(date, 1);
System.out.println(nextWeek); // 輸出:2023-01-08 10:30:00
// 日期比較
Date date1 = DateUtil.parse("2023-01-01");
Date date2 = DateUtil.parse("2023-02-01");
boolean isBefore = DateUtil.isBefore(date1, date2);
System.out.println(isBefore); // 輸出:true
}
}
UrlUtil工具類
UrlUtil工具類提供了對URL的處理方法,包括URL編碼、解碼、拼接等。
import cn.hutool.core.util.UrlUtil;
public class UrlUtilExample {
public static void main(String[] args) {
// URL編碼
String url = "https://www.example.com/search?keyword=Java";
String encodedUrl = UrlUtil.encode(url);
System.out.println(encodedUrl); // 輸出:https%3A%2F%2Fwww.example.com%2Fsearch%3Fkeyword%3DJava
// URL解碼
String encodedUrl = "https%3A%2F%2Fwww.example.com%2Fsearch%3Fkeyword%3DJava";
String decodedUrl = UrlUtil.decode(encodedUrl);
System.out.println(decodedUrl); // 輸出:https://www.example.com/search?keyword=Java
// 拼接URL參數
String baseUrl = "https://www.example.com/search";
String keyword = "Java";
String param = "page=1";
String fullUrl = UrlUtil.url(baseUrl).param("keyword", keyword).param(param).build();
System.out.println(fullUrl); // 輸出:https://www.example.com/search?keyword=Java&page=1
// 獲取URL中的域名
String url = "https://www.example.com/search?keyword=Java";
String domain = UrlUtil.getDomain(url);
System.out.println(domain); // 輸出:www.example.com
}
}
FileUtil工具類
FileUtil工具類提供了對文件和目錄的操作方法,簡化了文件的讀取、寫入和復制等操作。
import cn.hutool.core.io.FileUtil;
public class FileUtilExample {
public static void main(String[] args) {
// 讀取文件內容
String content = FileUtil.readUtf8String("path/to/file.txt");
System.out.println(content);
// 寫入文件內容
String content = "Hello, Hutool!";
FileUtil.writeUtf8String(content, "path/to/file.txt");
// 復制文件
FileUtil.copy("path/to/source.txt", "path/to/destination.txt", true);
// 刪除文件或目錄
FileUtil.del("path/to/file.txt");
}
}
總結
以上是Hutool工具類的四個常用示例,通過使用這些工具類,可以顯著簡化Java開發中的一些常見任務。希望本文對你學習和使用Hutool有所幫助,提升你的開發效率和代碼質量。