Apache Commons包估计是Java中使用最广发的工具包了,很多框架都依赖于这组工具包中的一部分,它提供了我们常用的一些编程需要,但是JDK没能提供的机能,最大化的减少重复代码的编写。
http://commons.apache.org/
1)Commons Lang是对JDK中java.lang包的补充,提供了各种各样的Utilities工具类,这里说说最常用的几个工具类。
版本:commons-lang3-3.1.jar
1、字符串的空判断
02 | System.out.println(StringUtils.isEmpty( null )); |
03 | System.out.println(StringUtils.isEmpty( "" )); |
04 | System.out.println(StringUtils.isEmpty( " " )); |
05 | System.out.println(StringUtils.isEmpty( "bob" )); |
06 | System.out.println(StringUtils.isEmpty( " bob " )); |
09 | System.out.println(StringUtils.isBlank( null )); |
10 | System.out.println(StringUtils.isBlank( "" )); |
11 | System.out.println(StringUtils.isBlank( " " )); |
12 | System.out.println(StringUtils.isBlank( "bob" )); |
13 | System.out.println(StringUtils.isBlank( " bob " )); |
2、字符串的Trim
02 | System.out.println(StringUtils.trim( null )); |
03 | System.out.println(StringUtils.trim( "" )); |
04 | System.out.println(StringUtils.trim( " " )); |
05 | System.out.println(StringUtils.trim( "abc" )); |
06 | System.out.println(StringUtils.trim( " abc" )); |
07 | System.out.println(StringUtils.trim( " abc " )); |
08 | System.out.println(StringUtils.trim( " ab c " )); |
11 | System.out.println(StringUtils.strip( null )); |
12 | System.out.println(StringUtils.strip( "" )); |
13 | System.out.println(StringUtils.strip( " " )); |
14 | System.out.println(StringUtils.strip( "abc" )); |
15 | System.out.println(StringUtils.strip( " abc" )); |
16 | System.out.println(StringUtils.strip( "abc " )); |
17 | System.out.println(StringUtils.strip( " abc " )); |
18 | System.out.println(StringUtils.strip( " ab c " )); |
20 | System.out.println(StringUtils.strip( " abcyx" , "xyz" )); |
22 | System.out.println(StringUtils.stripStart( "yxabcxyz " , "xyz" )); |
23 | System.out.println(StringUtils.stripEnd( " xyzabcyx" , "xyz" )); |
3、字符串的分割
02 | String str1 = "aaa bbb ccc" ; |
03 | String[] dim1 = StringUtils.split(str1); |
05 | System.out.println(dim1.length); |
06 | System.out.println(dim1[0]); |
07 | System.out.println(dim1[1]); |
08 | System.out.println(dim1[2]); |
11 | String str2 = "aaa,bbb,ccc" ; |
12 | String[] dim2 = StringUtils.split(str2, "," ); |
14 | System.out.println(dim2.length); |
15 | System.out.println(dim2[0]); |
16 | System.out.println(dim2[1]); |
17 | System.out.println(dim2[2]); |
20 | String str3 = "aaa,,bbb" ; |
21 | String[] dim3 = StringUtils.split(str3, "," ); |
23 | System.out.println(dim3.length); |
24 | System.out.println(dim3[0]); |
25 | System.out.println(dim3[1]); |
28 | String str4 = "aaa,,bbb" ; |
29 | String[] dim4 = StringUtils.splitPreserveAllTokens(str4, "," ); |
31 | System.out.println(dim4.length); |
32 | System.out.println(dim4[0]); |
33 | System.out.println(dim4[1]); |
34 | System.out.println(dim4[2]); |
37 | String str5 = "aaa,bbb,ccc" ; |
38 | String[] dim5 = StringUtils.split(str5, "," , 2); |
40 | System.out.println(dim5.length); |
41 | System.out.println(dim5[0]); |
42 | System.out.println(dim5[1]); |
4、字符串的连接
02 | String[] array = { "aaa" , "bbb" , "ccc" }; |
03 | String result1 = StringUtils.join(array, "," ); |
05 | System.out.println(result1); |
08 | List<String> list = new ArrayList<String>(); |
12 | String result2 = StringUtils.join(list, "," ); |
14 | System.out.println(result2); |
5、字符串的Escape
01 | System.out.println(StringEscapeUtils.escapeCsv( "测试测试哦" )); |
02 | System.out.println(StringEscapeUtils.escapeCsv( "测试,测试哦" )); |
03 | System.out.println(StringEscapeUtils.escapeCsv( "测试\n测试哦" )); |
05 | System.out.println(StringEscapeUtils.escapeHtml4("测试测试哦 |
07 | System.out.println(StringEscapeUtils.escapeJava( "\"rensaninng\",欢迎您!" )); |
09 | System.out.println(StringEscapeUtils.escapeEcmaScript( "测试'测试哦" )); |
10 | System.out.println(StringEscapeUtils.escapeXml( "<tt>\"bread\" & \"butter\"</tt>" )); |
6、随机数
02 | System.out.println(RandomStringUtils.randomAlphabetic(10)); |
05 | System.out.println(RandomStringUtils.randomAlphanumeric(10)); |
08 | System.out.println(RandomStringUtils.randomAscii(10)); |
11 | System.out.println(RandomStringUtils.random(10, "abcde" )); |
7、数组
02 | int[] array1 = {1, 2}; |
03 | array1 = ArrayUtils.add(array1, 3); |
05 | System.out.println(array1.length); |
06 | System.out.println(array1[2]); |
09 | int[] array2 = {1, 2, 3}; |
10 | array2 = ArrayUtils.remove(array2, 2); |
12 | System.out.println(array2.length); |
15 | int[] array3 = {1, 2, 3, 4}; |
16 | array3 = ArrayUtils.subarray(array3, 1, 3); |
18 | System.out.println(array3.length); |
21 | String[] array4 = { "aaa" , "bbb" , "ccc" }; |
22 | String[] copied = (String[]) ArrayUtils.clone(array4); |
24 | System.out.println(copied.length); |
27 | String[] array5 = { "aaa" , "bbb" , "ccc" , "bbb" }; |
28 | boolean result1 = ArrayUtils.contains(array5, "bbb" ); |
29 | System.out.println(result1); |
32 | int result2 = ArrayUtils.indexOf(array5, "bbb" ); |
33 | System.out.println(result2); |
36 | int result3 = ArrayUtils.lastIndexOf(array5, "bbb" ); |
37 | System.out.println(result3); |
40 | Map<Object, Object> map = ArrayUtils.toMap( new String[][]{ |
44 | System.out.println(map.get( "key1" )); |
45 | System.out.println(map.get( "key2" )); |
48 | Object[] array61 = new Object[0]; |
49 | Object[] array62 = null ; |
50 | Object[] array63 = new Object[]{ "aaa" }; |
52 | System.out.println(ArrayUtils.isEmpty(array61)); |
53 | System.out.println(ArrayUtils.isEmpty(array62)); |
54 | System.out.println(ArrayUtils.isNotEmpty(array63)); |
57 | Object[] array71 = new Object[]{ "aa" , "bb" , "cc" }; |
58 | Object[] array72 = new Object[]{ "dd" , "ee" , "ff" }; |
60 | System.out.println(ArrayUtils.isSameLength(array71, array72)); |
63 | Object[] array81 = new Object[]{ "aa" , "bb" , "cc" }; |
64 | Object[] array82 = new Object[]{ "aa" , "bb" , "cc" }; |
66 | System.out.println(ArrayUtils.isEquals(array81, array82)); |
69 | Integer[] array9 = new Integer[]{1, 2}; |
70 | int[] result = ArrayUtils.toPrimitive(array9); |
72 | System.out.println(result.length); |
73 | System.out.println(result[0]); |
76 | int[] array10 = new int[]{1, 2}; |
77 | Integer[] result10 = ArrayUtils.toObject(array10); |
79 | System.out.println(result.length); |
80 | System.out.println(result10[0].intValue()); |
8、日期
02 | Date date = DateUtils.parseDate( "2010/01/01 11:22:33" , new String[]{ "yyyy/MM/dd HH:mm:ss" }); |
05 | Date tenDaysAfter = DateUtils.addDays(date, 10); |
06 | System.out.println(DateFormatUtils.format(tenDaysAfter, "yyyy/MM/dd HH:mm:ss" )); |
09 | Date prevMonth = DateUtils.addMonths(date, -1); |
10 | System.out.println(DateFormatUtils.format(prevMonth, "yyyy/MM/dd HH:mm:ss" )); |
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)); |
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" ); |
5 | String name = BeanUtils.getProperty(bean1, "name" ); |
6 | String age = BeanUtils.getProperty(bean1, "age" ); |
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); |
5 | System.out.println(bean2.getName()); |
6 | System.out.println(bean2.getAge()); |
3、赋值Bean
1 | SampleBean bean3 = new SampleBean(); |
2 | bean3.setName( "rensanning" ); |
5 | SampleBean clone = (SampleBean) BeanUtils.cloneBean(bean3); |
7 | System.out.println(clone.getName()); |
8 | System.out.println(clone.getAge()); |
4、Bean的describe
1 | SampleBean bean4 = new SampleBean(); |
2 | bean4.setName( "rensanning" ); |
5 | @SuppressWarnings( "unchecked" ) |
6 | Map<String, String> map4 = BeanUtils.describe(bean4); |
8 | System.out.println(map4.get( "name" )); |
9 | System.out.println(map4.get( "age" )); |
5、Bean的populate
01 | SampleBean bean5 = new SampleBean(); |
03 | Map<String, String> map5 = new HashMap<String, String>(); |
04 | map5.put( "name" , "rensanning" ); |
07 | BeanUtils.populate(bean5, map5); |
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>(); |
09 | String[] array = BeanUtils.getArrayProperty(bean6, "array" ); |
11 | System.out.println(array.length); |
12 | System.out.println(array[0]); |
13 | System.out.println(array[1]); |
14 | System.out.println(array[2]); |
16 | String[] list = BeanUtils.getArrayProperty(bean6, "list" ); |
17 | System.out.println(list.length); |
18 | System.out.println(list[0]); |
19 | System.out.println(list[1]); |
20 | System.out.println(list[2]); |
22 | System.out.println(BeanUtils.getProperty(bean6, "array[1]" )); |
23 | System.out.println(BeanUtils.getIndexedProperty(bean6, "array" , 2)); |
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" ); |
07 | String value1 = BeanUtils.getMappedProperty(bean7, "map" , "key1" ); |
08 | System.out.println(value1); |
10 | String value2 = BeanUtils.getMappedProperty(bean7, "map" , "key2" ); |
11 | System.out.println(value2); |
13 | System.out.println(BeanUtils.getProperty(bean7, "map.key1" )); |
14 | System.out.println(BeanUtils.getProperty(bean7, "map.key2" )); |
8、获取Bean的嵌套字段值
1 | SampleBean bean = new SampleBean(); |
2 | NestedBean nestedBean = new NestedBean(); |
3 | nestedBean.setNestedProperty( "xxx" ); |
4 | bean.setNestedBean(nestedBean); |
6 | String value = BeanUtils.getNestedProperty(bean, "nestedBean.nestedProperty" ); |
7 | System.out.println(value); |
9 | System.out.println(BeanUtils.getProperty(bean, "nestedBean.nestedProperty" )); |
9、URL字段的特殊处理
1 | SampleBean bean8 = new SampleBean(); |
3 | BeanUtils.setProperty(bean8, "url" , "http://www.google.com/" ); |
5 | URL url = bean8.getUrl(); |
6 | System.out.println(url.getProtocol()); |
7 | System.out.println(url.getHost()); |
8 | System.out.println(url.getPath()); |
10、日期的转化
01 | SampleBean bean9 = new SampleBean(); |
03 | DateConverter converter = new DateConverter(); |
04 | converter.setPattern( "yyyy/MM/dd HH:mm:ss" ); |
06 | ConvertUtils.register(converter, Date.class); |
07 | ConvertUtils.register(converter, String.class); |
09 | BeanUtils.setProperty(bean9, "date" , "2010/12/19 23:40:00" ); |
11 | String value9 = BeanUtils.getProperty(bean9, "date" ); |
12 | System.out.println(value9); |
该类文章相关链接:
http://rensanning.iteye.com/blog/1548162Java集合框架之fastutil
http://rensanning.iteye.com/blog/1547845Java工具类之Apache的Commons Lang和BeanUtils
http://rensanning.iteye.com/blog/1546652Java日期计算之Joda-Time
http://rensanning.iteye.com/blog/1545708Java生成缩略图之Thumbnailator
http://rensanning.iteye.com/blog/1540613模板引擎之FreeMarker超入门
http://rensanning.iteye.com/blog/1540336Java构建工具之Ant超入门
http://rensanning.iteye.com/blog/1538689Java操作PDF之iText超入门
http://rensanning.iteye.com/blog/1538591Java读写Excel之POI超入门
转自:http://rensanning.iteye.com/blog/1547845