Apache Commons Lang是對java.lang的擴(kuò)展负蠕,基本上是commons中最常用的工具包成艘。
目前Lang包有兩個(gè)commons-lang3和commons-lang列敲。
lang最新版本是2.6糙捺,最低要求Java1.2以上咐扭,目前官方已不在維護(hù)例证。lang3目前最新版本是3.12.0懊渡,最低要求Java8以上膛薛。相對于lang來說完全支持Java8的特性听隐,廢除了一些舊的API。該版本無法兼容舊有版本哄啄,于是為了避免沖突改名為lang3雅任。
Java8以上的用戶推薦使用lang3代替lang风范,下面我們主要以lang3 - 3.12.0版本為例做說明。
以下為整體包結(jié)構(gòu):
org.apache.commons.lang3
org.apache.commons.lang3.builder
org.apache.commons.lang3.concurrent
org.apache.commons.lang3.event
org.apache.commons.lang3.exception
org.apache.commons.lang3.math
org.apache.commons.lang3.mutable
org.apache.commons.lang3.reflect
org.apache.commons.lang3.text
org.apache.commons.lang3.text.translate
org.apache.commons.lang3.time
org.apache.commons.lang3.tuple
下面只列舉其中常用的加以說明沪么,其余感興趣的可以自行翻閱源碼研究乌企。
01. 日期相關(guān)
在Java8之前,日期只提供了java.util.Date類和java.util.Calendar類成玫,說實(shí)話這些API并不是很好用加酵,而且也存在線程安全的問題,所以Java8推出了新的日期API哭当。如果你還在用舊的日期API猪腕,可以使用DateUtils和DateFormatUtils工具類。
1. 字符串轉(zhuǎn)日期
final String strDate = "2021-07-04 11:11:11";
final String pattern = "yyyy-MM-dd HH:mm:ss";
// 原生寫法
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date1 = sdf.parse(strDate);
// commons寫法
Date date2 = DateUtils.parseDate(strDate, pattern);
2. 日期轉(zhuǎn)字符串
final Date date = new Date();
final String pattern = "yyyy年MM月dd日";
// 原生寫法
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String strDate = sdf.format(date);
// 使用commons寫法
String strDate = DateFormatUtils.format(date, pattern);
3. 日期計(jì)算
final Date date = new Date();
// 原生寫法
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 5); // 加5天
cal.add(Calendar.HOUR_OF_DAY, -5); // 減5小時(shí)
// 使用commons寫法
Date newDate1 = DateUtils.addDays(date, 5); // 加5天
Date newDate2 = DateUtils.addHours(date, -5); // 減5小時(shí)
Date newDate3 = DateUtils.truncate(date, Calendar.DATE); // 過濾時(shí)分秒
boolean isSameDay = DateUtils.isSameDay(newDate1, newDate2); // 判斷是否是同一天
02. 字符串相關(guān)
字符串是Java中最常用的類型钦勘,相關(guān)的工具類也可以說是最常用的陋葡,下面直接看例子
1. 字符串判空
String str = "";
// 原生寫法
if (str == null || str.length() == 0) {
// Do something
}
// commons寫法
if (StringUtils.isEmpty(str)) {
// Do something
}
/* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
*/
相關(guān)方法:
// isEmpty取反
StringUtils.isNotEmpty(str);
/*
* null, 空串,空格為true
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
*/
StringUtils.isBlank(str);
// isBlank取反
StringUtils.isNotBlank(str);
// 任意一個(gè)參數(shù)為空則結(jié)果為true
StringUtils.isAnyEmpty(str1, str2, str3);
// 所有參數(shù)為空則結(jié)果為true
StringUtils.isAllEmpty(str1, str2, str3);
2. 字符串去空格
// 去除兩端空格彻采,不需要判斷null
String newStr = StringUtils.trim(str);
/*
* 去除兩端空格腐缤,如果是null則轉(zhuǎn)換為空字符串
* StringUtils.trimToEmpty(null) = ""
* StringUtils.trimToEmpty("") = ""
* StringUtils.trimToEmpty(" ") = ""
* StringUtils.trimToEmpty("abc") = "abc"
* StringUtils.trimToEmpty(" abc ") = "abc"
*/
newStr = StringUtils.trimToEmpty(str);
/*
* 去除兩端空格,如果結(jié)果是空串則轉(zhuǎn)換為null
* StringUtils.trimToNull(null) = null
* StringUtils.trimToNull("") = null
* StringUtils.trimToNull(" ") = null
* StringUtils.trimToNull("abc") = "abc"
* StringUtils.trimToNull(" abc ") = "abc"
*/
newStr = StringUtils.trimToNull(str);
/*
* 去兩端 給定字符串中任意字符
* StringUtils.strip(null, *) = null
* StringUtils.strip("", *) = ""
* StringUtils.strip("abc", null) = "abc"
* StringUtils.strip(" abc", null) = "abc"
* StringUtils.strip("abc ", null) = "abc"
* StringUtils.strip(" abc ", null) = "abc"
* StringUtils.strip(" abcyx", "xyz") = " abc"
*/
newStr = StringUtils.strip(str, "stripChars");
// 去左端 給定字符串中任意字符
newStr = StringUtils.stripStart(str, "stripChars");
// 去右端 給定字符串中任意字符
newStr = StringUtils.stripEnd(str, "stripChars");
3. 字符串分割
/*
* 按照空格分割字符串 結(jié)果為數(shù)組
* StringUtils.split(null) = null
* StringUtils.split("") = []
* StringUtils.split("abc def") = ["abc", "def"]
* StringUtils.split("abc def") = ["abc", "def"]
* tringUtils.split(" abc ") = ["abc"]
*/
StringUtils.split(str);
// 按照某些字符分割 結(jié)果為數(shù)組肛响,自動(dòng)去除了截取后的空字符串
StringUtils.split(str, ",");
4. 取子字符串
// 獲得"ab.cc.txt"中最后一個(gè).之前的字符串
StringUtils.substringBeforeLast("ab.cc.txt", "."); // ab.cc
// 相似方法
// 獲得"ab.cc.txt"中最后一個(gè).之后的字符串(常用于獲取文件后綴名)
StringUtils.substringAfterLast("ab.cc.txt", "."); // txt
// 獲得"ab.cc.txt"中第一個(gè).之前的字符串
StringUtils.substringBefore("ab.cc.txt", "."); // ab
// 獲得"ab.cc.txt"中第一個(gè).之后的字符串
StringUtils.substringAfter("ab.cc.txt", "."); // cc.txt
// 獲取"ab.cc.txt"中.之間的字符串
StringUtils.substringBetween("ab.cc.txt", "."); // cc
// 看名字和參數(shù)應(yīng)該就知道干什么的了
StringUtils.substringBetween("a(bb)c", "(", ")"); // bb
5. 其他
// 首字母大寫
StringUtils.capitalize("test"); // Test
// 字符串合并
StringUtils.join(new int[]{1,2,3}, ",");// 1,2,3
// 縮寫
StringUtils.abbreviate("abcdefg", 6);// "abc..."
// 判斷字符串是否是數(shù)字
StringUtils.isNumeric("abc123");// false
// 刪除指定字符
StringUtils.remove("abbc", "b"); // ac
// ... ... 還有很多岭粤,感興趣可以自己研究
6. 隨機(jī)字符串
// 隨機(jī)生成長度為5的字符串
RandomStringUtils.random(5);
// 隨機(jī)生成長度為5的"只含大小寫字母"字符串
RandomStringUtils.randomAlphabetic(5);
// 隨機(jī)生成長度為5的"只含大小寫字母和數(shù)字"字符串
RandomStringUtils.randomAlphanumeric(5);
// 隨機(jī)生成長度為5的"只含數(shù)字"字符串
RandomStringUtils.randomNumeric(5);
03. 反射相關(guān)
反射是Java中非要重要的特性,原生的反射API代碼冗長特笋,Lang包中反射相關(guān)的工具類可以很方便的實(shí)現(xiàn)反向相關(guān)功能剃浇,下面看例子
1. 屬性操作
public class ReflectDemo {
private static String sAbc = "111";
private String abc = "123";
public void fieldDemo() throws Exception {
ReflectDemo reflectDemo = new ReflectDemo();
// 反射獲取對象實(shí)例屬性的值
// 原生寫法
Field abcField = reflectDemo.getClass().getDeclaredField("abc");
abcField.setAccessible(true);// 設(shè)置訪問級別,如果private屬性不設(shè)置則訪問會(huì)報(bào)錯(cuò)
String value = (String) abcField.get(reflectDemo);// 123
// commons寫法
String value2 = (String) FieldUtils.readDeclaredField(reflectDemo, "abc", true);//123
// 方法名如果不含Declared會(huì)向父類上一直查找
}
}
注:方法名含Declared的只會(huì)在當(dāng)前類實(shí)例上尋找猎物,不包含Declared的在當(dāng)前類上找不到則會(huì)遞歸向父類上一直查找虎囚。
相關(guān)方法:
public class ReflectDemo {
private static String sAbc = "111";
private String abc = "123";
public void fieldRelated() throws Exception {
ReflectDemo reflectDemo = new ReflectDemo();
// 反射獲取對象屬性的值
String value2 = (String) FieldUtils.readField(reflectDemo, "abc", true);//123
// 反射獲取類靜態(tài)屬性的值
String value3 = (String) FieldUtils.readStaticField(ReflectDemo.class, "sAbc", true);//111
// 反射設(shè)置對象屬性值
FieldUtils.writeField(reflectDemo, "abc", "newValue", true);
// 反射設(shè)置類靜態(tài)屬性的值
FieldUtils.writeStaticField(ReflectDemo.class, "sAbc", "newStaticValue", true);
}
}
2. 獲取注解方法
// 獲取被Test注解標(biāo)識(shí)的方法
// 原生寫法
List<Method> annotatedMethods = new ArrayList<Method>();
for (Method method : ReflectDemo.class.getMethods()) {
if (method.getAnnotation(Test.class) != null) {
annotatedMethods.add(method);
}
}
// commons寫法
Method[] methods = MethodUtils.getMethodsWithAnnotation(ReflectDemo.class, Test.class);
3. 方法調(diào)用
private static void testStaticMethod(String param1) {}
private void testMethod(String param1) {}
public void invokeDemo() throws Exception {
// 調(diào)用函數(shù)"testMethod"
ReflectDemo reflectDemo = new ReflectDemo();
// 原生寫法
Method testMethod = reflectDemo.getClass().getDeclaredMethod("testMethod");
testMethod.setAccessible(true); // 設(shè)置訪問級別,如果private函數(shù)不設(shè)置則調(diào)用會(huì)報(bào)錯(cuò)
testMethod.invoke(reflectDemo, "testParam");
// commons寫法
MethodUtils.invokeExactMethod(reflectDemo, "testMethod", "testParam");
// ---------- 類似方法 ----------
// 調(diào)用static方法
MethodUtils.invokeExactStaticMethod(ReflectDemo.class, "testStaticMethod", "testParam");
// 調(diào)用方法(含繼承過來的方法)
MethodUtils.invokeMethod(reflectDemo, "testMethod", "testParam");
// 調(diào)用static方法(當(dāng)前不存在則向父類尋找匹配的靜態(tài)方法)
MethodUtils.invokeStaticMethod(ReflectDemo.class, "testStaticMethod", "testParam");
}
其他還有ClassUtils蔫磨,ConstructorUtils淘讥,TypeUtils等不是很常用,有需求的可以現(xiàn)翻看類的源碼堤如。
04. 系統(tǒng)相關(guān)
主要是獲取操作系統(tǒng)和JVM一些信息蒲列,下面看例子
// 判斷操作系統(tǒng)類型
boolean isWin = SystemUtils.IS_OS_WINDOWS;
boolean isWin10 = SystemUtils.IS_OS_WINDOWS_10;
boolean isWin2012 = SystemUtils.IS_OS_WINDOWS_2012;
boolean isMac = SystemUtils.IS_OS_MAC;
boolean isLinux = SystemUtils.IS_OS_LINUX;
boolean isUnix = SystemUtils.IS_OS_UNIX;
boolean isSolaris = SystemUtils.IS_OS_SOLARIS;
// ... ...
// 判斷java版本
boolean isJava6 = SystemUtils.IS_JAVA_1_6;
boolean isJava8 = SystemUtils.IS_JAVA_1_8;
boolean isJava11 = SystemUtils.IS_JAVA_11;
boolean isJava14 = SystemUtils.IS_JAVA_14;
// ... ...
// 獲取java相關(guān)目錄
File javaHome = SystemUtils.getJavaHome();
File userHome = SystemUtils.getUserHome();// 操作系統(tǒng)用戶目錄
File userDir = SystemUtils.getUserDir();// 項(xiàng)目所在路徑
File tmpDir = SystemUtils.getJavaIoTmpDir();
05. 總結(jié)
除了以上介紹的工具類外,還有其他不是很常用的就不多做介紹了煤惩。感興趣的可以自行翻閱源碼研究嫉嘀。
后續(xù)章節(jié)我將繼續(xù)給大家介紹commons中其他好用的工具類庫,期待你的關(guān)注魄揉。