http://commons.apache.org/
1)Commons Lang是對JDK中java.lang包的補充,提供了各種各樣的Utilities工具類,這裏說說最常用的幾個工具類。
版本:commons-lang3-3.1.jar
1、字符串的空判斷
01 |
//isEmpty |
02 |
System.out.println(StringUtils.isEmpty( null )); //
true |
03 |
System.out.println(StringUtils.isEmpty( "" )); //
true |
04 |
System.out.println(StringUtils.isEmpty( "
" )); //
false |
05 |
System.out.println(StringUtils.isEmpty( "bob" )); //
false |
06 |
System.out.println(StringUtils.isEmpty( "
bob " )); //
false |
07 |
08 |
//isBlank |
09 |
System.out.println(StringUtils.isBlank( null )); //
true |
10 |
System.out.println(StringUtils.isBlank( "" )); //
true |
11 |
System.out.println(StringUtils.isBlank( "
" )); //
true |
12 |
System.out.println(StringUtils.isBlank( "bob" )); //
false |
13 |
System.out.println(StringUtils.isBlank( "
bob " )); //
false |
2、字符串的Trim
01 |
//trim |
02 |
System.out.println(StringUtils.trim( null )); //
null |
03 |
System.out.println(StringUtils.trim( "" )); //
"" |
04 |
System.out.println(StringUtils.trim( "
" )); //
"" |
05 |
System.out.println(StringUtils.trim( "abc" )); //
"abc" |
06 |
System.out.println(StringUtils.trim( "
abc" )); //
"abc" |
07 |
System.out.println(StringUtils.trim( "
abc " )); //
"abc" |
08 |
System.out.println(StringUtils.trim( "
ab c " )); //
"ab c" |
09 |
10 |
//strip |
11 |
System.out.println(StringUtils.strip( null )); //
null |
12 |
System.out.println(StringUtils.strip( "" )); //
"" |
13 |
System.out.println(StringUtils.strip( "
" )); //
"" |
14 |
System.out.println(StringUtils.strip( "abc" )); //
"abc" |
15 |
System.out.println(StringUtils.strip( "
abc" )); //
"abc" |
16 |
System.out.println(StringUtils.strip( "abc
" )); //
"abc" |
17 |
System.out.println(StringUtils.strip( "
abc " )); //
"abc" |
18 |
System.out.println(StringUtils.strip( "
ab c " )); //
"ab c" |
19 |
|
20 |
System.out.println(StringUtils.strip( "
abcyx" , "xyz" )); //
" abc" |
21 |
22 |
System.out.println(StringUtils.stripStart( "yxabcxyz
" , "xyz" )); //
"abcxyz " |
23 |
System.out.println(StringUtils.stripEnd( "
xyzabcyx" , "xyz" )); //
" xyzabc" |
3、字符串的分割
01 |
//默認半角空格分割 |
02 |
String
str1 = "aaa
bbb ccc" ; |
03 |
String[]
dim1 = StringUtils.split(str1); //
=> ["aaa", "bbb", "ccc"] |
04 |
05 |
System.out.println(dim1.length); //3 |
06 |
System.out.println(dim1[0]); //"aaa" |
07 |
System.out.println(dim1[1]); //"bbb" |
08 |
System.out.println(dim1[2]); //"ccc" |
09 |
10 |
//指定分隔符 |
11 |
String
str2 = "aaa,bbb,ccc" ; |
12 |
String[]
dim2 = StringUtils.split(str2, "," ); //
=> ["aaa", "bbb", "ccc"] |
13 |
14 |
System.out.println(dim2.length); //3 |
15 |
System.out.println(dim2[0]); //"aaa" |
16 |
System.out.println(dim2[1]); //"bbb" |
17 |
System.out.println(dim2[2]); //"ccc" |
18 |
19 |
//去除空字符串 |
20 |
String
str3 = "aaa,,bbb" ; |
21 |
String[]
dim3 = StringUtils.split(str3, "," ); //
=> ["aaa", "bbb"] |
22 |
23 |
System.out.println(dim3.length); //2 |
24 |
System.out.println(dim3[0]); //"aaa" |
25 |
System.out.println(dim3[1]); //"bbb" |
26 |
27 |
//包含空字符串 |
28 |
String
str4 = "aaa,,bbb" ; |
29 |
String[]
dim4 = StringUtils.splitPreserveAllTokens(str4, "," ); //
=> ["aaa", "", "bbb"] |
30 |
31 |
System.out.println(dim4.length); //3 |
32 |
System.out.println(dim4[0]); //"aaa" |
33 |
System.out.println(dim4[1]); //"" |
34 |
System.out.println(dim4[2]); //"bbb" |
35 |
36 |
//指定分割的最大次數(超過後不分割) |
37 |
String
str5 = "aaa,bbb,ccc" ; |
38 |
String[]
dim5 = StringUtils.split(str5, "," ,
2); //
=> ["aaa", "bbb,ccc"] |
39 |
40 |
System.out.println(dim5.length); //2 |
41 |
System.out.println(dim5[0]); //"aaa" |
42 |
System.out.println(dim5[1]); //"bbb,ccc" |
4、字符串的連接
01 |
//數組元素拼接 |
02 |
String[]
array = { "aaa" , "bbb" , "ccc" }; |
03 |
String
result1 = StringUtils.join(array, "," ); |
04 |
05 |
System.out.println(result1); //"aaa,bbb,ccc" |
06 |
07 |
//集合元素拼接 |
08 |
List<String>
list = new ArrayList<String>(); |
09 |
list.add( "aaa" ); |
10 |
list.add( "bbb" ); |
11 |
list.add( "ccc" ); |
12 |
String
result2 = StringUtils.join(list, "," ); |
13 |
14 |
System.out.println(result2); //"aaa,bbb,ccc" |
5、字符串的Escape
01 |
System.out.println(StringEscapeUtils.escapeCsv( "測試測試哦" )); //"測試測試哦" |
02 |
System.out.println(StringEscapeUtils.escapeCsv( "測試,測試哦" )); //"\"測試,測試哦\"" |
03 |
System.out.println(StringEscapeUtils.escapeCsv( "測試\n測試哦" )); //"\"測試\n測試哦\"" |
04 |
05 |
System.out.println(StringEscapeUtils.escapeHtml4("測試測試哦 |
06 |
"));//" <p>測試測試哦</p>" |
07 |
System.out.println(StringEscapeUtils.escapeJava( "\"rensaninng\",歡迎您!" )); //"\"rensaninng\"\uFF0C\u6B22\u8FCE\u60A8\uFF01" |
08 |
09 |
System.out.println(StringEscapeUtils.escapeEcmaScript( "測試'測試哦" )); //"\u6D4B\u8BD5\'\u6D4B\u8BD5\u54E6" |
10 |
System.out.println(StringEscapeUtils.escapeXml( "<tt>\"bread\"
& \"butter\"</tt>" )); //"<tt>"bread"
& "butter"</tt>" |
6、隨機數
01 |
//
10位英字 |
02 |
System.out.println(RandomStringUtils.randomAlphabetic(10)); |
03 |
04 |
//
10位英數 |
05 |
System.out.println(RandomStringUtils.randomAlphanumeric(10)); |
06 |
07 |
//
10位ASCII碼 |
08 |
System.out.println(RandomStringUtils.randomAscii(10)); |
09 |
10 |
//
指定文字10位 |
11 |
System.out.println(RandomStringUtils.random(10, "abcde" )); |
7、數組
01 |
//
追加元素到數組尾部 |
02 |
int[]
array1 = {1, 2}; |
03 |
array1
= ArrayUtils.add(array1, 3); //
=> [1, 2, 3] |
04 |
05 |
System.out.println(array1.length); //3 |
06 |
System.out.println(array1[2]); //3 |
07 |
08 |
//
刪除指定位置的元素 |
09 |
int[]
array2 = {1, 2, 3}; |
10 |
array2
= ArrayUtils.remove(array2, 2); //
=> [1, 2] |
11 |
12 |
System.out.println(array2.length); //2 |
13 |
14 |
//
截取部分元素 |
15 |
int[]
array3 = {1, 2, 3, 4}; |
16 |
array3
= ArrayUtils.subarray(array3, 1, 3); //
=> [2, 3] |
17 |
18 |
System.out.println(array3.length); //2 |
19 |
20 |
//
數組拷貝 |
21 |
String[]
array4 = { "aaa" , "bbb" , "ccc" }; |
22 |
String[]
copied = (String[]) ArrayUtils.clone(array4); //
=> {"aaa", "bbb", "ccc"} |
23 |
|
24 |
System.out.println(copied.length); //3 |
25 |
26 |
//
判斷是否包含某元素 |
27 |
String[]
array5 = { "aaa" , "bbb" , "ccc" , "bbb" }; |
28 |
boolean
result1 = ArrayUtils.contains(array5, "bbb" ); //
=> true |
29 |
System.out.println(result1); //true |
30 |
31 |
//
判斷某元素在數組中出現的位置(從前往後,沒有返回-1) |
32 |
int
result2 = ArrayUtils.indexOf(array5, "bbb" ); //
=> 1 |
33 |
System.out.println(result2); //1 |
34 |
35 |
//
判斷某元素在數組中出現的位置(從後往前,沒有返回-1) |
36 |
int
result3 = ArrayUtils.lastIndexOf(array5, "bbb" ); //
=> 3 |
37 |
System.out.println(result3); //3 |
38 |
39 |
//
數組轉Map |
40 |
Map<Object,
Object> map = ArrayUtils.toMap( new String[][]{ |
41 |
{ "key1" , "value1" }, |
42 |
{ "key2" , "value2" } |
43 |
}); |
44 |
System.out.println(map.get( "key1" )); //"value1" |
45 |
System.out.println(map.get( "key2" )); //"value2" |
46 |
47 |
//
判斷數組是否爲空 |
48 |
Object[]
array61 = new Object[0]; |
49 |
Object[]
array62 = null ; |
50 |
Object[]
array63 = new Object[]{ "aaa" }; |
51 |
52 |
System.out.println(ArrayUtils.isEmpty(array61)); //true |
53 |
System.out.println(ArrayUtils.isEmpty(array62)); //true |
54 |
System.out.println(ArrayUtils.isNotEmpty(array63)); //true |
55 |
56 |
//
判斷數組長度是否相等 |
57 |
Object[]
array71 = new Object[]{ "aa" , "bb" , "cc" }; |
58 |
Object[]
array72 = new Object[]{ "dd" , "ee" , "ff" }; |
59 |
60 |
System.out.println(ArrayUtils.isSameLength(array71,
array72)); //true |
61 |
62 |
//
判斷數組元素內容是否相等 |
63 |
Object[]
array81 = new Object[]{ "aa" , "bb" , "cc" }; |
64 |
Object[]
array82 = new Object[]{ "aa" , "bb" , "cc" }; |
65 |
66 |
System.out.println(ArrayUtils.isEquals(array81,
array82)); |
67 |
68 |
//
Integer[] 轉化爲 int[] |
69 |
Integer[]
array9 = new Integer[]{1,
2}; |
70 |
int[]
result = ArrayUtils.toPrimitive(array9); |
71 |
72 |
System.out.println(result.length); //2 |
73 |
System.out.println(result[0]); //1 |
74 |
75 |
//
int[] 轉化爲 Integer[] |
76 |
int[]
array10 = new int[]{1,
2}; |
77 |
Integer[]
result10 = ArrayUtils.toObject(array10); |
78 |
79 |
System.out.println(result.length); //2 |
80 |
System.out.println(result10[0].intValue()); //1 |
8、日期
01 |
//
生成Date對象 |
02 |
Date
date = DateUtils.parseDate( "2010/01/01
11:22:33" , new String[]{ "yyyy/MM/dd
HH:mm:ss" }); |
03 |
04 |
//
10天后 |
05 |
Date
tenDaysAfter = DateUtils.addDays(date, 10); //
=> 2010/01/11 11:22:33 |
06 |
System.out.println(DateFormatUtils.format(tenDaysAfter, "yyyy/MM/dd
HH:mm:ss" )); |
07 |
08 |
//
前一個月 |
09 |
Date
prevMonth = DateUtils.addMonths(date, -1); //
=> 2009/12/01 11:22:33 |
10 |
System.out.println(DateFormatUtils.format(prevMonth, "yyyy/MM/dd
HH:mm:ss" )); |
11 |
12 |
//
判斷是否是同一天 |
13 |
Date
date1 = DateUtils.parseDate( "2010/01/01
11:22:33" , new String[]{ "yyyy/MM/dd
HH:mm:ss" }); |
14 |
Date
date2 = DateUtils.parseDate( "2010/01/01
22:33:44" , new String[]{ "yyyy/MM/dd
HH:mm:ss" }); |
15 |
System.out.println(DateUtils.isSameDay(date1,
date2)); //
true |
16 |
17 |
//
日期格式化 |
18 |
System.out.println(DateFormatUtils.format( new Date(), "yyyy/MM/dd
HH:mm:ss" )); |
2)Commons Beanutils是一組專門用於操作Bean的工具類,目前很多流行的框架基本都離不開他。
版本:commons-beanutils-1.8.3.jar
1、獲取字段值
1 |
SampleBean
bean1 = new SampleBean(); |
2 |
bean1.setName( "rensanning" ); |
3 |
bean1.setAge(31); |
4 |
5 |
String
name = BeanUtils.getProperty(bean1, "name" ); |
6 |
String
age = BeanUtils.getProperty(bean1, "age" ); |
7 |
8 |
System.out.println(name); |
9 |
System.out.println(age); |
2、設置字段值
1 |
SampleBean
bean2 = new SampleBean(); |
2 |
BeanUtils.setProperty(bean2, "name" , "rensanning" ); |
3 |
BeanUtils.setProperty(bean2, "age" ,
31); |
4 |
5 |
System.out.println(bean2.getName()); |
6 |
System.out.println(bean2.getAge()); |
3、賦值Bean
1 |
SampleBean
bean3 = new SampleBean(); |
2 |
bean3.setName( "rensanning" ); |
3 |
bean3.setAge(31); |
4 |
5 |
SampleBean
clone = (SampleBean) BeanUtils.cloneBean(bean3); |
6 |
7 |
System.out.println(clone.getName()); |
8 |
System.out.println(clone.getAge()); |
4、Bean的describe
1 |
SampleBean
bean4 = new SampleBean(); |
2 |
bean4.setName( "rensanning" ); |
3 |
bean4.setAge(31); |
4 |
5 |
@SuppressWarnings( "unchecked" ) |
6 |
Map<String,
String> map4 = BeanUtils.describe(bean4); |
7 |
8 |
System.out.println(map4.get( "name" )); |
9 |
System.out.println(map4.get( "age" )); |
5、Bean的populate
01 |
SampleBean
bean5 = new SampleBean(); |
02 |
03 |
Map<String,
String> map5 = new HashMap<String,
String>(); |
04 |
map5.put( "name" , "rensanning" ); |
05 |
map5.put( "age" , "31" ); |
06 |
07 |
BeanUtils.populate(bean5,
map5); |
08 |
09 |
System.out.println(bean5.getName()); |
10 |
System.out.println(bean5.getAge()); |
6、獲取Bean的數組集合字段值
01 |
SampleBean
bean6 = new SampleBean(); |
02 |
bean6.setArray( new String[]{ "a" , "b" , "c" }); |
03 |
List<String>
list0 = new ArrayList<String>(); |
04 |
list0.add( "d" ); |
05 |
list0.add( "e" ); |
06 |
list0.add( "f" ); |
07 |
bean6.setList(list0); |
08 |
09 |
String[]
array = BeanUtils.getArrayProperty(bean6, "array" ); |
10 |
11 |
System.out.println(array.length); //3 |
12 |
System.out.println(array[0]); //"a" |
13 |
System.out.println(array[1]); //"b" |
14 |
System.out.println(array[2]); //"c" |
15 |
16 |
String[]
list = BeanUtils.getArrayProperty(bean6, "list" ); |
17 |
System.out.println(list.length); //3 |
18 |
System.out.println(list[0]); //"d" |
19 |
System.out.println(list[1]); //"e" |
20 |
System.out.println(list[2]); //"f" |
21 |
22 |
System.out.println(BeanUtils.getProperty(bean6, "array[1]" )); //"b" |
23 |
System.out.println(BeanUtils.getIndexedProperty(bean6, "array" ,
2)); //"c" |
7、獲取Bean的Map字段值
01 |
SampleBean
bean7 = new SampleBean(); |
02 |
Map<String,
String> map = new HashMap<String,
String>(); |
03 |
map.put( "key1" , "value1" ); |
04 |
map.put( "key2" , "value2" ); |
05 |
bean7.setMap(map); |
06 |
07 |
String
value1 = BeanUtils.getMappedProperty(bean7, "map" , "key1" ); |
08 |
System.out.println(value1); //"value1" |
09 |
10 |
String
value2 = BeanUtils.getMappedProperty(bean7, "map" , "key2" ); |
11 |
System.out.println(value2); //"value2" |
12 |
13 |
System.out.println(BeanUtils.getProperty(bean7, "map.key1" )); //"value1" |
14 |
System.out.println(BeanUtils.getProperty(bean7, "map.key2" )); //"value2" |
8、獲取Bean的嵌套字段值
1 |
SampleBean
bean = new SampleBean(); |
2 |
NestedBean
nestedBean = new NestedBean(); |
3 |
nestedBean.setNestedProperty( "xxx" ); |
4 |
bean.setNestedBean(nestedBean); |
5 |
6 |
String
value = BeanUtils.getNestedProperty(bean, "nestedBean.nestedProperty" ); |
7 |
System.out.println(value); //"xxx" |
8 |
9 |
System.out.println(BeanUtils.getProperty(bean, "nestedBean.nestedProperty" )); //"xxx" |
9、URL字段的特殊處理
1 |
SampleBean
bean8 = new SampleBean(); |
2 |
3 |
BeanUtils.setProperty(bean8, "url" , "http://www.google.com/" ); |
4 |
5 |
URL
url = bean8.getUrl(); |
6 |
System.out.println(url.getProtocol()); //"http" |
7 |
System.out.println(url.getHost()); //"www.google.com" |
8 |
System.out.println(url.getPath()); //"/" |
10、日期的轉化
01 |
SampleBean
bean9 = new SampleBean(); |
02 |
03 |
DateConverter
converter = new DateConverter(); |
04 |
converter.setPattern( "yyyy/MM/dd
HH:mm:ss" ); |
05 |
06 |
ConvertUtils.register(converter,
Date.class); |
07 |
ConvertUtils.register(converter,
String.class); |
08 |
09 |
BeanUtils.setProperty(bean9, "date" , "2010/12/19
23:40:00" ); |
10 |
11 |
String
value9 = BeanUtils.getProperty(bean9, "date" ); |
12 |
System.out.println(value9); //"2010/12/19
23:40:00" |