Apache Commons包估计是Java中使用最广发的工具包了,很多框架都依赖于这组工具包中的一部分,它提供了我们常用的一些编程需要,但是JDK没能提供的机能,最大化的减少重复代码的编写。
http://commons.apache.org/
1)Commons Lang是对JDK中java.lang包的补充,提供了各种各样的Utilities工具类,这里说说最常用的几个工具类。
版本:commons-lang3-3.1.jar
1、字符串的空判断
2、字符串的Trim
3、字符串的分割
4、字符串的连接
5、字符串的Escape
6、随机数
7、数组
8、日期
2)Commons Beanutils是一组专门用于操作Bean的工具类,目前很多流行的框架基本都离不开他。
版本:commons-beanutils-1.8.3.jar
1、获取字段值
2、设置字段值
3、赋值Bean
4、Bean的describe
5、Bean的populate
6、获取Bean的数组集合字段值
7、获取Bean的Map字段值
8、获取Bean的嵌套字段值
9、URL字段的特殊处理
10、日期的转化
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" |