commons-lang包中對(duì)我們有用的類主要有: 1.StringUtils 該類主要提供對(duì)字符串的操作,對(duì)null是安全的,主要提供了字符串查找,替換,分割,去空白,去掉非法字符等等操作 2.ObjectUtils 主要是對(duì)null進(jìn)行安全處理,可以設(shè)置為null時(shí)的默認(rèn)返回值,比較相等時(shí)是調(diào)用對(duì)象的equals方法,因此需要對(duì)對(duì)象進(jìn)行方法進(jìn)行覆蓋 3.SystemUtils 主要獲取一些系統(tǒng)屬性,例如工作目錄等等 4.DateUtils/CalendarUtils 主要提供了對(duì)日期的操作,包括日期加減,日期格式化,日期比較,一定時(shí)間范圍內(nèi)日期的迭代等等 5.StopWatch 提供秒表的計(jì)時(shí),暫停等功能 6. EqualsBuilder/HashCodeBuilder提供了方便的方法來覆蓋equals() 和hashCode()方法 7.以Range結(jié)尾的類主要提供一些范圍的操作,包括判斷某些字符,數(shù)字等是否在這個(gè)范圍以內(nèi) 8.ArrayUtils 提供了數(shù)組的復(fù)制,查找,獲取子數(shù)組,反轉(zhuǎn)等功能 Apache Commons包估計(jì)是Java中使用最廣發(fā)的工具包了,,很多框架都依賴于這組工具包中的一部分,,它提供了我們常用的一些編程需要,但是JDK沒能提供的機(jī)能,,最大化的減少重復(fù)代碼的編寫,。 http://commons./ 1)Commons Lang是對(duì)JDK中java.lang包的補(bǔ)充,,提供了各種各樣的Utilities工具類,,這里說說最常用的幾個(gè)工具類。
版本:commons-lang3-3.1.jar 1,、字符串的空判斷 -
- System.out.println(StringUtils.isEmpty(null));
- System.out.println(StringUtils.isEmpty(""));
- System.out.println(StringUtils.isEmpty(" "));
- System.out.println(StringUtils.isEmpty("bob"));
- System.out.println(StringUtils.isEmpty(" bob "));
-
-
- System.out.println(StringUtils.isBlank(null));
- System.out.println(StringUtils.isBlank(""));
- System.out.println(StringUtils.isBlank(" "));
- System.out.println(StringUtils.isBlank("bob"));
- System.out.println(StringUtils.isBlank(" bob "));
2,、字符串的Trim -
- System.out.println(StringUtils.trim(null));
- System.out.println(StringUtils.trim(""));
- System.out.println(StringUtils.trim(" "));
- System.out.println(StringUtils.trim("abc"));
- System.out.println(StringUtils.trim(" abc"));
- System.out.println(StringUtils.trim(" abc "));
- System.out.println(StringUtils.trim(" ab c "));
-
-
- System.out.println(StringUtils.strip(null));
- System.out.println(StringUtils.strip(""));
- System.out.println(StringUtils.strip(" "));
- System.out.println(StringUtils.strip("abc"));
- System.out.println(StringUtils.strip(" abc"));
- System.out.println(StringUtils.strip("abc "));
- System.out.println(StringUtils.strip(" abc "));
- System.out.println(StringUtils.strip(" ab c "));
-
- System.out.println(StringUtils.strip(" abcyx", "xyz"));
-
- System.out.println(StringUtils.stripStart("yxabcxyz ", "xyz"));
- System.out.println(StringUtils.stripEnd(" xyzabcyx", "xyz"));
3、字符串的分割 -
- String str1 = "aaa bbb ccc";
- String[] dim1 = StringUtils.split(str1);
-
- System.out.println(dim1.length);
- System.out.println(dim1[0]);
- System.out.println(dim1[1]);
- System.out.println(dim1[2]);
-
-
- String str2 = "aaa,bbb,ccc";
- String[] dim2 = StringUtils.split(str2, ",");
-
- System.out.println(dim2.length);
- System.out.println(dim2[0]);
- System.out.println(dim2[1]);
- System.out.println(dim2[2]);
-
-
- String str3 = "aaa,,bbb";
- String[] dim3 = StringUtils.split(str3, ",");
-
- System.out.println(dim3.length);
- System.out.println(dim3[0]);
- System.out.println(dim3[1]);
-
-
- String str4 = "aaa,,bbb";
- String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ",");
-
- System.out.println(dim4.length);
- System.out.println(dim4[0]);
- System.out.println(dim4[1]);
- System.out.println(dim4[2]);
-
-
- String str5 = "aaa,bbb,ccc";
- String[] dim5 = StringUtils.split(str5, ",", 2);
-
- System.out.println(dim5.length);
- System.out.println(dim5[0]);
- System.out.println(dim5[1]);
4,、字符串的連接 -
- String[] array = {"aaa", "bbb", "ccc"};
- String result1 = StringUtils.join(array, ",");
-
- System.out.println(result1);
-
-
- List<String> list = new ArrayList<String>();
- list.add("aaa");
- list.add("bbb");
- list.add("ccc");
- String result2 = StringUtils.join(list, ",");
-
- System.out.println(result2);
5,、字符串的Escape - System.out.println(StringEscapeUtils.escapeCsv("測(cè)試測(cè)試哦"));
- System.out.println(StringEscapeUtils.escapeCsv("測(cè)試,測(cè)試哦"));
- System.out.println(StringEscapeUtils.escapeCsv("測(cè)試\n測(cè)試哦"));
-
- System.out.println(StringEscapeUtils.escapeHtml4("測(cè)試測(cè)試哦
- "));//"<p>測(cè)試測(cè)試哦</p>"
- System.out.println(StringEscapeUtils.escapeJava("\"rensaninng\",歡迎您,!"));
-
- System.out.println(StringEscapeUtils.escapeEcmaScript("測(cè)試'測(cè)試哦"));
- System.out.println(StringEscapeUtils.escapeXml("<tt>\"bread\" & \"butter\"</tt>"));
6,、隨機(jī)數(shù) -
- System.out.println(RandomStringUtils.randomAlphabetic(10));
-
-
- System.out.println(RandomStringUtils.randomAlphanumeric(10));
-
-
- System.out.println(RandomStringUtils.randomAscii(10));
-
-
- System.out.println(RandomStringUtils.random(10, "abcde"));
7、數(shù)組 -
- int[] array1 = {1, 2};
- array1 = ArrayUtils.add(array1, 3);
-
- System.out.println(array1.length);
- System.out.println(array1[2]);
-
-
- int[] array2 = {1, 2, 3};
- array2 = ArrayUtils.remove(array2, 2);
-
- System.out.println(array2.length);
-
-
- int[] array3 = {1, 2, 3, 4};
- array3 = ArrayUtils.subarray(array3, 1, 3);
-
- System.out.println(array3.length);
-
-
- String[] array4 = {"aaa", "bbb", "ccc"};
- String[] copied = (String[]) ArrayUtils.clone(array4);
-
- System.out.println(copied.length);
-
-
- String[] array5 = {"aaa", "bbb", "ccc", "bbb"};
- boolean result1 = ArrayUtils.contains(array5, "bbb");
- System.out.println(result1);
-
-
- int result2 = ArrayUtils.indexOf(array5, "bbb");
- System.out.println(result2);
-
-
- int result3 = ArrayUtils.lastIndexOf(array5, "bbb");
- System.out.println(result3);
-
-
- Map<Object, Object> map = ArrayUtils.toMap(new String[][]{
- {"key1", "value1"},
- {"key2", "value2"}
- });
- System.out.println(map.get("key1"));
- System.out.println(map.get("key2"));
-
-
- Object[] array61 = new Object[0];
- Object[] array62 = null;
- Object[] array63 = new Object[]{"aaa"};
-
- System.out.println(ArrayUtils.isEmpty(array61));
- System.out.println(ArrayUtils.isEmpty(array62));
- System.out.println(ArrayUtils.isNotEmpty(array63));
-
-
- Object[] array71 = new Object[]{"aa", "bb", "cc"};
- Object[] array72 = new Object[]{"dd", "ee", "ff"};
-
- System.out.println(ArrayUtils.isSameLength(array71, array72));
-
-
- Object[] array81 = new Object[]{"aa", "bb", "cc"};
- Object[] array82 = new Object[]{"aa", "bb", "cc"};
-
- System.out.println(ArrayUtils.isEquals(array81, array82));
-
-
- Integer[] array9 = new Integer[]{1, 2};
- int[] result = ArrayUtils.toPrimitive(array9);
-
- System.out.println(result.length);
- System.out.println(result[0]);
-
-
- int[] array10 = new int[]{1, 2};
- Integer[] result10 = ArrayUtils.toObject(array10);
-
- System.out.println(result.length);
- System.out.println(result10[0].intValue());
8、日期 -
- Date date = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});
-
-
- Date tenDaysAfter = DateUtils.addDays(date, 10);
- System.out.println(DateFormatUtils.format(tenDaysAfter, "yyyy/MM/dd HH:mm:ss"));
-
-
- Date prevMonth = DateUtils.addMonths(date, -1);
- System.out.println(DateFormatUtils.format(prevMonth, "yyyy/MM/dd HH:mm:ss"));
-
-
- Date date1 = DateUtils.parseDate("2010/01/01 11:22:33", new String[]{"yyyy/MM/dd HH:mm:ss"});
- Date date2 = DateUtils.parseDate("2010/01/01 22:33:44", new String[]{"yyyy/MM/dd HH:mm:ss"});
- System.out.println(DateUtils.isSameDay(date1, date2));
-
-
- System.out.println(DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss"));
2)Commons Beanutils是一組專門用于操作Bean的工具類,,目前很多流行的框架基本都離不開他,。
版本:commons-beanutils-1.8.3.jar 1、獲取字段值 - SampleBean bean1 = new SampleBean();
- bean1.setName("rensanning");
- bean1.setAge(31);
-
- String name = BeanUtils.getProperty(bean1, "name");
- String age = BeanUtils.getProperty(bean1, "age");
-
- System.out.println(name);
- System.out.println(age);
2,、設(shè)置字段值 - SampleBean bean2 = new SampleBean();
- BeanUtils.setProperty(bean2, "name", "rensanning");
- BeanUtils.setProperty(bean2, "age", 31);
-
- System.out.println(bean2.getName());
- System.out.println(bean2.getAge());
3,、賦值Bean - SampleBean bean3 = new SampleBean();
- bean3.setName("rensanning");
- bean3.setAge(31);
-
- SampleBean clone = (SampleBean) BeanUtils.cloneBean(bean3);
-
- System.out.println(clone.getName());
- System.out.println(clone.getAge());
4、Bean的describe - SampleBean bean4 = new SampleBean();
- bean4.setName("rensanning");
- bean4.setAge(31);
-
- @SuppressWarnings("unchecked")
- Map<String, String> map4 = BeanUtils.describe(bean4);
-
- System.out.println(map4.get("name"));
- System.out.println(map4.get("age"));
5,、Bean的populate - SampleBean bean5 = new SampleBean();
-
- Map<String, String> map5 = new HashMap<String, String>();
- map5.put("name", "rensanning");
- map5.put("age", "31");
-
- BeanUtils.populate(bean5, map5);
-
- System.out.println(bean5.getName());
- System.out.println(bean5.getAge());
6,、獲取Bean的數(shù)組集合字段值 - SampleBean bean6 = new SampleBean();
- bean6.setArray(new String[]{"a", "b", "c"});
- List<String> list0 = new ArrayList<String>();
- list0.add("d");
- list0.add("e");
- list0.add("f");
- bean6.setList(list0);
-
- String[] array = BeanUtils.getArrayProperty(bean6, "array");
-
- System.out.println(array.length);
- System.out.println(array[0]);
- System.out.println(array[1]);
- System.out.println(array[2]);
-
- String[] list = BeanUtils.getArrayProperty(bean6, "list");
- System.out.println(list.length);
- System.out.println(list[0]);
- System.out.println(list[1]);
- System.out.println(list[2]);
-
- System.out.println(BeanUtils.getProperty(bean6, "array[1]"));
- System.out.println(BeanUtils.getIndexedProperty(bean6, "array", 2));
7、獲取Bean的Map字段值 - SampleBean bean7 = new SampleBean();
- Map<String, String> map = new HashMap<String, String>();
- map.put("key1", "value1");
- map.put("key2", "value2");
- bean7.setMap(map);
-
- String value1 = BeanUtils.getMappedProperty(bean7, "map", "key1");
- System.out.println(value1);
-
- String value2 = BeanUtils.getMappedProperty(bean7, "map", "key2");
- System.out.println(value2);
-
- System.out.println(BeanUtils.getProperty(bean7, "map.key1"));
- System.out.println(BeanUtils.getProperty(bean7, "map.key2"));
8,、獲取Bean的嵌套字段值 - SampleBean bean = new SampleBean();
- NestedBean nestedBean = new NestedBean();
- nestedBean.setNestedProperty("xxx");
- bean.setNestedBean(nestedBean);
-
- String value = BeanUtils.getNestedProperty(bean, "nestedBean.nestedProperty");
- System.out.println(value);
-
- System.out.println(BeanUtils.getProperty(bean, "nestedBean.nestedProperty"));
9,、URL字段的特殊處理 - SampleBean bean8 = new SampleBean();
-
- BeanUtils.setProperty(bean8, "url", "http://www.google.com/");
-
- URL url = bean8.getUrl();
- System.out.println(url.getProtocol());
- System.out.println(url.getHost());
- System.out.println(url.getPath());
10、日期的轉(zhuǎn)化 - SampleBean bean9 = new SampleBean();
-
- DateConverter converter = new DateConverter();
- converter.setPattern("yyyy/MM/dd HH:mm:ss");
-
- ConvertUtils.register(converter, Date.class);
- ConvertUtils.register(converter, String.class);
-
- BeanUtils.setProperty(bean9, "date", "2010/12/19 23:40:00");
-
- String value9 = BeanUtils.getProperty(bean9, "date");
- System.out.println(value9);
Jodd的BeanUtil也提供了相當(dāng)方便的操作Bean的API,。
|