Commons-lang API介绍

4.1 Commons-lang API介绍

 

4.1.1 StringUtils

4.1.2 StringEscapeUtils

4.1.3 ArrayUtils

4.1.4 DateUtils

4.1.5 DateFormatUtils

4.1.6 RandomUtils

4.1.7 NumberUtils

4.1.8 FieldUtils

4.1.9 CharUtils

4.1.10 BooleanUtils

  4.1.11 ExceptionUtils

 

 

 

1    StringUtils方法介绍

 

StringUtils是提供字符串操作的工具类。提供的方法如下:

1 public static boolean isEmpty(String str);

说明:

如果参数str为NULL或者str.length() == 0 返回true

对比:JDK 中类String的方法public boolean isEmpty()

此方法通过判断私有变量count是否等于0来进行判断。

StringUtils.isEmpty(null) = true 

StringUtils.isEmpty("") = true 

StringUtils.isEmpty(" ") = false 

StringUtils.isEmpty("        ")  = false 

StringUtils.isEmpty("aa") = false 

StringUtils.isEmpty(" aaa ") = false 

2 public static boolean isNotEmpty(String str)

说明:

判断给定参数是否不为空,其实现方式利用了方法一: !isEmpty(str);

对比:JDK中String类无此方法。

StringUtils.isNotEmpty(null);//false  
StringUtils.isNotEmpty("");//false  
StringUtils.isNotEmpty(" ");//true  
StringUtils.isNotEmpty("         ");//true  
StringUtils.isNotEmpty("aa");//true  
StringUtils.isNotEmpty(" aaa ");//true

public static boolean isBlank(String str)

说明:

如果参数str为NULL或者其长度等于0,又或者其由空格组成,那么此方法都返回true。

对比:JDK中String类无此方法。

System.out.println(StringUtils.isBlank(null));//true
System.out.println(StringUtils.isBlank(""));//true
System.out.println(StringUtils.isBlank(" "));//true
System.out.println(StringUtils.isBlank("   "));//true
System.out.println(StringUtils.isBlank("\n\t"));//true
System.out.println(StringUtils.isBlank("aaa"));//false
System.out.println(StringUtils.isBlank(" aa "));//false

public static boolean isNotBlank(String str)

说明:利用方法三实现。 

 

5 public static String trim(String str)

说明:

去除字符串开头和结尾处的空格字符。如果参数str为null,则返回null.

对比:

利用JDK中String类的trim()方法。

//去空格.Null返回null~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
System.out.println(StringUtils.trim(null));

//去空格,将Null和"" 转换为Null
System.out.println(StringUtils.trimToNull(""));
//去空格,将NULL 和 "" 转换为""
System.out.println(StringUtils.trimToEmpty(null));

 

6 public static String stripStart(String str, String stripChars)

说明:

去掉str前端的在stripChars中的字符

//如果第二个参数为null只去前面空格(否则去掉字符串前面一样的字符,到不一样为止)
System.out.println(StringUtils.stripStart("ddsuuu ", "d"));

 

7 public static String stripEnd(String str, String stripChars)

说明:

去掉str末端的在stripChars中的字符

//如果第二个参数为null只去后面空格,(否则去掉字符串后面一样的字符,到不一样为止)
System.out.println(StringUtils.stripEnd("dabads", "das"));

//如果第二个参数为null去空格(否则去掉字符串2边一样的字符,到不一样为止)
System.out.println(StringUtils.strip("fsfsdf", "f"));

 

//检查是否查到,返回boolean,null返回假
System.out.println(StringUtils.contains("sdf", "dg"));
//检查是否查到,返回boolean,null返回假,不区分大小写
System.out.println(StringUtils.containsIgnoreCase("sdf", "D"));
//检查是否有含有空格,返回boolean
System.out.println(StringUtils.containsWhitespace(" d"));

8 public static int ordinalIndexOf(String str, String searchStr, int ordinal)

说明:

返回字符串search在字符串str中第ordinal次出现的位置。

如果str=null或searchStr=null或ordinal<=0则返回-1.

//从指定位置(三参数)开始查找,本例从第2个字符开始查找k字符
System.out.println(StringUtils.indexOf("akfekcd中华", "k", 2));
//未发现不同之处
System.out.println(StringUtils.ordinalIndexOf("akfekcd中华", "k", 2));

9. StringUtils.defaultIfEmpty(String str, String defalutValue)

如果字符串为""或者 null 则替换成参数2中的字符串:

System.out.println("1: " + StringUtils.defaultIfEmpty("", "a"));//1: a

System.out.println("2: " + StringUtils.defaultIfEmpty("\n\t", "a"));//2: 
System.out.println("3: " + StringUtils.defaultIfEmpty("", "a"));//3: a
System.out.println("4: " + StringUtils.defaultIfEmpty("   ", "a"));//4:    
System.out.println("5: " + StringUtils.defaultIfEmpty("aaa", "a"));//5: aaa
System.out.println("6: " + StringUtils.defaultIfEmpty(" aaa ", "a"));//6:  aaa 
System.out.println("7: " + StringUtils.defaultIfEmpty(null, "a"));//7: a

 

10.  StringUtils.defaultString(String str, String defaultValue)

  和9相似:

System.out.println("1: " + StringUtils.defaultString(null, "a"));//1: a
System.out.println("2: " + StringUtils.defaultString("", "a"));//2: 
System.out.println("3: " + StringUtils.defaultString(" ", "a"));//3:  
System.out.println("4: " + StringUtils.defaultString("\n\t", "a"));//4: 
System.out.println("5: " + StringUtils.defaultString("aa", "a"));//5: aa
System.out.println("6: " + StringUtils.defaultString(" aaa", "a"));//6:  aaa

 

11. StringUtils.capitalize(String str)

首字母大写

System.out.println(StringUtils.capitalize("xxxx"));//Xxxx

System.out.println(StringUtils.capitalize("Xxxx"));//Xxxx


12. StringUtils.remove(String str, String removeStr)

从str中去除removeStr

System.out.println(StringUtils.remove("abcd", "ab"));//cd
System.out.println(StringUtils.remove("abcd", "ad"));//abcd

13. StringUtils.countMatches(String str, String find)

计算字符串在另一个字符串中出现的次数:

int nCount = StringUtils.countMatches("UPDATE tb_table SET xx=?,xyz=?, sss=? WHERE id=?", "?");//nCount = 4

http://www.cnblogs.com/jifeng/archive/2012/08/05/2623767.html

 

 


 

4.1.2 StringEscapeUtils

StringEscapeUtils这个类里提供了很多转义的方法,比如可以转成json、xml、html等格式。

 

1.escapeJava/unescapeJava 把字符串转为unicode编码

 

package com.kungeek.tip;

import org.apache.commons.lang.StringEscapeUtils;


public class test {
    
    public static void main(String args[]) {
        String sql = "1' or '1'='1";
        // 防SQL注入 防SQL注入:1'' or ''1''=''1
        System.out.println("防SQL注入:" + StringEscapeUtils.escapeSql(sql));
        
        // 转义成Unicode编码 转成Unicode编码:\u9648\u78CA\u5174 
        String unicode="陈磊兴";
        String esc_unicode=StringEscapeUtils.escapeJava(unicode);
        String unesc_unicode=StringEscapeUtils.unescapeJava(esc_unicode);
        System.out.println("转成Unicode编码:" + esc_unicode);
        System.out.println("反转成Unicode编码:" + unesc_unicode);
        
        String html="<font>chen磊  xing</font>";
        String esc_html=StringEscapeUtils.escapeHtml(html);
        String unesc_html=StringEscapeUtils.unescapeHtml(esc_html);
        // 转义HTML
        System.out.println("转义HTML:" + esc_html);
        // 反转义HTML 
        System.out.println("反转义HTML:" + unesc_html);
        
        String xml="<name>陈磊兴</name>";
        String esc_xml=StringEscapeUtils.escapeHtml(xml);
        String unesc_xml=StringEscapeUtils.unescapeXml(esc_xml);
        // 转义xml 转义XML:
        System.out.println("转义XML:" + esc_xml);
        // 转义xml 反转义XML:
        System.out.println("反转义XML:" +unesc_xml );

    }

}

 

 

 

 


 

 

4.1.3 ArrayUtils

 

package chongqingyusp;

import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

public class test {

    public static void main(String[] args) {

        // 1.打印数组
        String a=ArrayUtils.toString(new int[] { 1,4, 2, 3 });// {1,4,2,3}
        String a1=ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 });// {1,4,2,3}
        String a2=ArrayUtils.toString(null, "I'm nothing!");// I'm nothing!
        
        System.out.println("a----------"+a);
        System.out.println("a1----------"+a1);
        System.out.println("a2----------"+a2);
        System.out.println("-----------------------------------------");
        // 2.判断两个数组是否相等,采用EqualsBuilder进行判断
        // 只有当两个数组的数据类型,长度,数值顺序都相同的时候,该方法才会返回True
        // 2.1 两个数组完全相同
        boolean b=ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
        // 2.2 数据类型以及长度相同,但各个Index上的数据不是一一对应
        boolean b1=ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });//false
        // 2.3 数组的长度不一致
        boolean b2=ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });//false
        // 2.4 不同的数据类型
        boolean b3=ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new long[] { 1, 2, 3 });// false
        boolean b4=ArrayUtils.isEquals(new Object[] { 1, 2, 3 }, new Object[] { 1, (long) 2, 3 });// false
        // 2.5 Null处理,如果输入的两个数组都为null时候则返回true
        boolean b5=ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
        boolean b6=ArrayUtils.isEquals(null, null);// true
        
        System.out.println("b----------"+b);
        System.out.println("b1----------"+b1);
        System.out.println("b2----------"+b2);
        System.out.println("b3----------"+b3);
        System.out.println("b4----------"+b4);
        System.out.println("b5----------"+b5);
        System.out.println("b6----------"+b6);
        System.out.println("-----------------------------------------");
        
        // 3.将一个数组转换成Map
        // 如果数组里是Entry则其Key与Value就是新Map的Key和Value,如果是Object[]则Object[0]为KeyObject[1]为Value
        // 对于Object[]数组里的元素必须是instanceof Object[]或者Entry,即不支持基本数据类型数组
        // 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})会出异常
        Map c=ArrayUtils.toMap(new Object[] { new Object[] { 1, 2 }, new Object[] { 3, 4 } });// {1=2,
        // 3=4}
        Map c1=ArrayUtils.toMap(new Integer[][] { new Integer[] { 1, 2 }, new Integer[] { 3, 4 } });// {1=2,
        // 3=4}
        
        System.out.println("c----------"+c);
        System.out.println("c1----------"+c1);
        System.out.println("-----------------------------------------");
        
        // 4.拷贝数组
        int[] d=ArrayUtils.clone(new int[] { 3, 2, 4 });// {3,2,4}
        System.out.println("d----------"+ArrayUtils.toString(d));
        System.out.println("-----------------------------------------");
        
        // 5.截取数组
        int[] e=ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 4);// {1,5}
        System.out.println("e----------"+ArrayUtils.toString(e));
        // 起始index为2(即第三个数据)结束index为4的数组
        int[] e1=ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10);// {1,5,6}
        // 如果endIndex大于数组的长度,则取beginIndex之后的所有数据
        System.out.println("e1----------"+ArrayUtils.toString(e1));
        System.out.println("-----------------------------------------");
        
        // 6.判断两个数组的长度是否相等
        boolean f=ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true
        System.out.println("f----------"+f);
        System.out.println("-----------------------------------------");
        
        // 7.获得数组的长度
        int g=ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3
        System.out.println("g----------"+g);
        System.out.println("-----------------------------------------");
        
        // 8.判段两个数组的类型是否相同
        boolean h=ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 });// true
        boolean h1=ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 });// false
        System.out.println("h----------"+h);
        System.out.println("h----------"+h1);
        System.out.println("-----------------------------------------");
        
        // 9.数组反转
        int[] array =new int[] { 1, 2, 5 };
        ArrayUtils.reverse(array);// {5,2,1}
        System.out.println("i----------"+ArrayUtils.toString(array));
        System.out.println("-----------------------------------------");
        
        // 10.查询某个Object在数组中的位置,可以指定起始搜索位置,找不到返回-1
        // 10.1 从正序开始搜索,搜到就返回当前的index否则返回-1
        int j=ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6);// 2
        int j1=ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2);// -1
        // 10.2 从逆序开始搜索,搜到就返回当前的index否则返回-1
        int j2=ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6);// 2
        
        System.out.println("j----------"+ArrayUtils.toString(j));
        System.out.println("j1----------"+ArrayUtils.toString(j1));
        System.out.println("j2----------"+ArrayUtils.toString(j2));
        System.out.println("-----------------------------------------");
        
        
        // 11.查询某个Object是否在数组中
        boolean k=ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
        // 对于Object数据是调用该Object.equals方法进行判断
        boolean k1=ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L);// false
        System.out.println("k----------"+ArrayUtils.toString(k));
        System.out.println("k1----------"+ArrayUtils.toString(k1));
        System.out.println("-----------------------------------------");
        

        // 12.基本数据类型数组与外包型数据类型数组互转
        Integer[] l=ArrayUtils.toObject(new int[] { 1, 2 });// new Integer[]{Integer,Integer}
        int[] l1=ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) });// new int[]{1,2}
        System.out.println("l----------"+ArrayUtils.toString(l));
        System.out.println("l1----------"+ArrayUtils.toString(l1));
        System.out.println("-----------------------------------------");
        
        // 13.判断数组是否为空(null和length=0的时候都为空)
        boolean m=ArrayUtils.isEmpty(new int[0]);// true
        boolean m1=ArrayUtils.isEmpty(new Object[] { null });// false
        System.out.println("m----------"+ArrayUtils.toString(m));
        System.out.println("m1----------"+ArrayUtils.toString(m1));
        System.out.println("-----------------------------------------");
        
        // 14.合并两个数组
        int[] o=ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}
        System.out.println("o----------"+ArrayUtils.toString(o));
        System.out.println("-----------------------------------------");
        
        // 15.添加一个数据到数组
        int[] p=ArrayUtils.add(new int[] { 1, 3, 5 }, 4);// {1,3,5,4}
        System.out.println("p----------"+ArrayUtils.toString(p));
        System.out.println("-----------------------------------------");
        
        // 16.删除数组中某个位置上的数据
        int[] q=ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
        System.out.println("q----------"+ArrayUtils.toString(q));
        System.out.println("-----------------------------------------");
        
        // 17.删除数组中某个对象(从正序开始搜索,删除第一个)
        int[] r=ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}
        System.out.println("r----------"+ArrayUtils.toString(r));
        System.out.println("-----------------------------------------");
        
        
        
        
        
        
        
        
    }

}

结果

a----------{1,4,2,3}
a1----------{1,4,2,3}
a2----------I'm nothing!
-----------------------------------------
b----------true
b1----------false
b2----------false
b3----------false
b4----------false
b5----------false
b6----------true
-----------------------------------------
c----------{1=2, 3=4}
c1----------{1=2, 3=4}
-----------------------------------------
d----------{3,2,4}
-----------------------------------------
e----------{1,5}
e1----------{1,5,6}
-----------------------------------------
f----------true
-----------------------------------------
g----------3
-----------------------------------------
h----------true
h----------false
-----------------------------------------
i----------{5,2,1}
-----------------------------------------
j----------2
j1-----------1
j2----------2
-----------------------------------------
k----------true
k1----------false
-----------------------------------------
l----------{1,2}
l1----------{1,2}
-----------------------------------------
m----------true
m1----------false
-----------------------------------------
o----------{1,3,5,2,4}
-----------------------------------------
p----------{1,3,5,4}
-----------------------------------------
q----------{1,5}
-----------------------------------------
r----------{1,5}

-----------------------------------------

 

 


 

4.1.4 DateUtils

 

package com.kungeek.tip;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

public class test {
    
    public static void main(String[] args) {
        
        Date now = new Date();
        System.out.println("now:"+format(now));
 
        //addYears
        System.out.println("addYears:"+format(DateUtils.addYears(now, 1)));
        //addMonths
        System.out.println("addMonths:"+format(DateUtils.addMonths(now, 1)));        
        //addDays
        System.out.println("addDays:"+format(DateUtils.addDays(now, 1)));
        //addHours
        System.out.println("addHours:"+format(DateUtils.addHours(now, 1)));
        //addMinutes
        System.out.println("addMinutes:"+format(DateUtils.addMinutes(now, 1)));
        //addSeconds
        System.out.println("addSeconds:"+format(DateUtils.addSeconds(now, 1)));
        //addMilliseconds
        System.out.println("addMilliseconds:"+DateUtils.addMilliseconds(now, 1));
        //-------------------------------------------------------------------------------------
        //isSameDay
        System.out.println("isSameDay:"+DateUtils.isSameDay(now, DateUtils.addYears(now, 1)));
        System.out.println("isSameDay:"+DateUtils.isSameDay(now, Calendar.getInstance().getTime()));
        //-------------------------------------------------------------------------------------
        //round
        System.out.println("round:"+format(DateUtils.round(now, 1)));
        System.out.println("round:"+format(DateUtils.round(now, 2)));
        //truncate
        System.out.println("truncate:"+format(DateUtils.truncate(now, 1)));     
        System.out.println("truncate:"+format(DateUtils.truncate(now, 2)));
        //-------------------------------------------------------------------------------------
    }

    private static String format(Date date){
        return DateFormatUtils.format(date,"yyyy-MM-dd-HH:mm:ss");
    }
    
    
}

 

 


 

4.1.5 DateFormatUtils

    与SUN的SimpleDateFormat相比,其主要优点是:线程安全。

    对应于SimpleDateFormat的format()的方法,是DateFormatUtils 的format系列方法,常用的就是:

public static java.lang.String format (java.util.Date date, java.lang.String pattern);

DateFormatUtils定义了很多内置的固定日期格式,均为FastDateFormat类型,比如 ISO_DATE_FORMAT。使用 FastDateFormat的format()方法可以直接将日期格式化为内置的固定格式。

public java.lang.String format (java.util.Date date)

常用日期格式的格式化操作:

1: 以 yyyy-MM-dd 格式化:
DateFormatUtils.ISO_DATE_FORMAT.format(new Date()): 2009-03-20

2:以 yyyy-MM-ddZZ 格式化:

DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(new Date()): 2009-03-20+08:00

3:以 yyyy-MM-dd'T'HH:mm:ss 格式化:
DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date()): 2009-03-20T22:07:01

4: 以 yyyy-MM-dd'T'HH:mm:ssZZ 格式化:
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(new Date()): 2009-03-20T22:07:01+08:00

5: 以 'T'HH:mm:ss 格式化:
DateFormatUtils.ISO_TIME_FORMAT.format(new Date()): T22:07:01

6: 以 HH:mm:ss 格式化:
DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(new Date()): 22:07:01

7: 以 HH:mm:ssZZ 格式化:
DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(new Date()): 22:07:01+08:00

8: 以 'T'HH:mm:ssZZ 格式化:
DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(new Date()): T22:07:01+08:00

自定义日期格式的格式化操作: 

1: 以 yyyy-MM-dd HH:mm:ss 格式化Date对象:
DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"): 2009-03-20 22:24:30

2: 以 yyyy-MM-dd HH:mm:ss 格式化Calendar对象:
DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"): 2009-03-20 22:24:30

3: 以 yyyy-MM-dd HH:mm:ss 格式化TimeInMillis:
DateFormatUtils.format(Calendar.getInstance().getTimeInMillis(), "yyyy-MM-dd HH:mm:ss"): 2009-03-20 22:24:30

http://blog.csdn.net/spring_0534/article/details/6256991

 

package com.kungeek.tip;

import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;


public class test {

    
    public static void main(String args[]){
        Date date = new Date();
        System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_TIME_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(date));
        System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(date) + " " + DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(date));
        
        System.out.println("--------------------------------");
        //----------------------------自定义模版    
        System.out.println(DateFormatUtils.format(date,"yyyyMMdd"));
        System.out.println(DateFormatUtils.format(date,"yyyy-MM-dd-HH-mm-ss"));
        
        System.out.println(DateFormatUtils.format(date,"yyyyMMddhhmmss"));//12小时制
        System.out.println(DateFormatUtils.format(date,"yyyyMMddHHmmss"));//24小时制
        
    }
    
}

 

 

4.1.6 RandomUtils

    随机数据生成类,包括浮点,双精,布尔,整形,长整在内的随机数生成

RandomUtils.nextInt();采用默认的JVMRandom类,数值范围0~2147483647

nextInt(Random random);也可以设置其他的random

还支持以下方法:

nextLong();

nextBoolean();

nextFloat();

nextDouble();

RandomUtils.nextBoolean();  
RandomUtils.nextDouble();  
RandomUtils.nextLong();  
// 注意这里传入的参数不是随机种子,而是在0~1000之间产生一位随机数  
RandomUtils.nextInt(1000);

 

 

 

4.1.7   NumberUtils

为JDK中的Number类提供额外的功能。

提供可复用的值为0,1的数值型的包装类。包括Long、Integer、Short、Byte、Double、Float。

/*1.NumberUtils.isNumber():判断字符串是否是数字*/
NumberUtils.isNumber("5.96");//结果是true
NumberUtils.isNumber("s5");//结果是false
NumberUtils.isNumber("0000000000596");//结果是true
/*2.NumberUtils.isDigits():判断字符串中是否全为数字*/
NumberUtils.isDigits("0000000000.596");//false
NumberUtils.isDigits("0000000000596");//true
/*3.NumberUtils.toInt():字符串转换为整数*/
NumberUtils.toInt("5");
NumberUtils.toLong("5");
NumberUtils.toByte("3");
NumberUtils.toFloat("3.2");
NumberUtils.toDouble("4");
NumberUtils.toShort("3");
/*4.NumberUtils.max():找出最大的一个*/
NumberUtils.max(newint[]{3,5,6});//结果是6
NumberUtils.max(3,1,7);//结果是7
 
/*5.NumberUtils.min():找出最小的一个*/
NumberUtils.min(newint[]{3,5,6});//结果是6
NumberUtils.min(3,1,7);//结果是7
 
/*6.NumberUtils.createBigDecimal()通过字符串创建BigDecimal类型,支持long、int、float、double、number等数值*/
NumberUtils.createBigDecimal("1");
NumberUtils.createLong("1");
NumberUtils.createInteger("1");

http://www.cnblogs.com/linjiqin/archive/2013/11/14/3423856.html

4.1.8 FieldUtils

通过反射技术来操作成员变量。

 

1 getField

Field getField(Class<?> ,String [,Boolean]);

Field getDeclaredField(Class<?>,String [,Boolean]);

说明:

getField:   Gets an accessible Field by name breaking scope if requested. 当前类的接口和和父类都会被考虑。

getDeclaredField : an accessible Field by name respecting scope.

仅当前类被考虑。

http://jw-long.iteye.com/blog/1838178

http://www.yiibai.com/javalang/class_getfield.html

2   readField

readStaticField(Field[,boolean]);

   readStaticField(Class<?>,String[,boolean]);

   readDeclaredStaticField(Class<?>,String[,boolean]);

   readField(Field,Object[,boolean]);

   readField(Object,String[,boolean]);

   readDeclaredField(Object,String[,boolean])

获取字段的值。区别是:带有Declared的仅考虑当前类,其他情况会考虑当前类实现的接口以及其父类。

 

3   writeField

writeStaticField(Field,Object[,boolean]);

writeStaticField(Class<?>,String,Object[,boolean]);

writeDeclaredStaticField(Class<?>,String,Object[,boolean]);

writeField(Field,Object,Object[,boolean]);

writeField(Object, String, Object[,boolean]);

writeDeclaredField(Object, String, Object[,boolean]);

设置字段的值.

 

4.1.8    CharUtils

静态类,不需要创建

1 public static boolean isAscii(char ch)

用途:判断是否为ascii字符

实现:

public static Boolean isAscii(char ch){

    return ch < 128;

}

2 public static boolean isAsciiAlpha(char ch)

用途:判断是否为ascii字母,即值在65到90或者97到122

实现:

public static Boolean isAsciiAlpha(char ch) {

   return (ch >= ‘A’&& ch <= ‘Z’) || (ch >= ‘a’&& CH <= ‘z’)

}

3 public static Boolean isAsciiAlphaLower(char ch)

同2. 判断范围是 65 到 90,即a 到 z.

4 public static boolean isAsciiAlphaUpper(char ch);

同2.判断范围是97到122.即A 到Z.

 

5 public static boolean isAsciiAlphanumeric(char ch)

用途:判断是否为ascii字符数字,即值在48到57,65到90或者97到122.

实现:

Public static boolean isAsciiAlphanumeric(char ch) {

       return (ch >= ‘A’&& cn <= ‘Z’) || (ch >= ‘a’&& ch <= ‘z’) || (ch >= ‘0’&& ch <= ‘9’)

}

6  public static Boolean isAsciiControl(char ch)

用途:判断是否为控制字符

实现:

public static boolean isAsciiControl(char ch) {

        return ch < 32 || ch = 127;

}

7 public static boolean isAsciiNumeric(char ch)

用途:判断是否为数字

实现:

Public static Boolean isAsciiNumeric(char ch) {

       Return ch >= ‘0’ && ch <= ‘9’;

}

8 public static Boolean isAsciiPrintable(char ch)

用途:判断是否可打印出得ascii字符

实现:

Public static Boolean isAsciiPrintable(char ch) {

       return ch >= 32 && ch < 127;

}

9 public static int toIntValue(char ch)

用途:数字转换

实现:

Public static int toIntValue(char ch) {

        if(isAsciiNumeric(ch) == false) {

       throw new IllegalArgumentException(“the character” + ch + “is not in the range ‘0’- ‘9’”)

}

Return ch – 48;

}

10 public static String unicodeEscaped(char ch)

用途:将ch转换为unicode表示的字符串形式

实现:

public static String unicodeEscaped(char ch) {

      if(ch < 0x10) {

     return “\\u000” + Integer.toHexString(ch);

  }else if(ch < 0x100){

     retrun  “\\u00” + Integer.toHexString(ch);

  }else if(ch < 0x1000) {

     Return “\\u0” + Integer.toHexString(ch);

  }

  Return “\\u” + Integer.toHexString(ch);

}

 

 

 

4.1.10 BooleanUtils 

 

1  negate(Boolean bool)

用法:否定指定的boolean值

 

2  isTrue(Boolean bool)

用法:检查一个boolean值是否为true,如果参数为null,返回false

 

3  isNotTrue(Boolean bool)

用法:检查一个boolean值是否为false,如果参数为null,返回true

 

4  isFalse(Boolean bool)

用法:检查一个boolean值是否为false,如果是 返回true.

      如果检查的值为true或null返回false.

 

5  isNotFalse(Boolean bool)

用法:检查一个boolean值是否不为false,如果是返回true

 

6  toBoolean(Boolean bool)

用法:转换一个为null的boolean值,返回一个false.

         * <pre>

         *   BooleanUtils.toBoolean(Boolean.TRUE)  = true

         *   BooleanUtils.toBoolean(Boolean.FALSE) = false

         *   BooleanUtils.toBoolean(null)          = false

         * </pre>

 

7  toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)

用法: 转换一个为null的boolean值,返回后面参数给定的boolean值.

         * <pre>

         *   BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true

         *   BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false

         *   BooleanUtils.toBooleanDefaultIfNull(null, true)          = true

         * </pre>

         */

 

8  toBoolean(int value)

用法: 当参数为0是返回false,其它都返回true.

         * <pre>

         *   BooleanUtils.toBoolean(0) = false

         *   BooleanUtils.toBoolean(1) = true

         *   BooleanUtils.toBoolean(2) = true

         * </pre>

 

9  toBooleanObject(int value)

用法:  当参数为0是返回Boolean.FALSE对象,其它都返回Boolean.TRUE.

         * <pre>

         *   BooleanUtils.toBoolean(0) = Boolean.FALSE

         *   BooleanUtils.toBoolean(1) = Boolean.TRUE

         *   BooleanUtils.toBoolean(2) = Boolean.TRUE

         * </pre>

 

10  toBooleanObject(Integer value)

用法: 当参数为0是返回Boolean.FALSE对象,为null返回null

         * 其它则返回Boolean.TRUE.

         * <pre>

         *   BooleanUtils.toBoolean(new Integer(0))    = Boolean.FALSE

         *   BooleanUtils.toBoolean(new Integer(1))    = Boolean.TRUE

         *   BooleanUtils.toBoolean(new Integer(null)) = null

         * </pre>

 

11  toBoolean(int value, int trueValue, int falseValue)

用法:   * 如果第一个参数和第二个参数相等返回true,

         * 如果第一个参数和第三个参数相等返回false,

         * 如果都没有相等的,返回一个IllegalArgumentException

         * * <pre>

         *   BooleanUtils.toBoolean(0, 1, 0) = false

         *   BooleanUtils.toBoolean(1, 1, 0) = true

         *   BooleanUtils.toBoolean(2, 1, 2) = false

         *   BooleanUtils.toBoolean(2, 2, 0) = true

         * </pre>

 

 

4.1.11 ExceptionUtils

对异常的常见操作,获得堆栈,异常抛出方法名,错误链中对象数

 

public static Throwable getCause(Throwable [,String[]]);

用法:获取导致Throwable的Throwable. 可以设置自己制定的方法名称.

 

public static Throwable getRootCause(Throwable throwable);

用法:获取导致Throwable的 Root Throwable。

 

 

public static int getThrowableCount(Throwable throwable)

用法:统计异常链上的Throwable对象数量。

A throwable without cause will return 1;

A throwable with one cause will return 2 and so on

A null throwable will return 0.

 

 

 

 

 

posted @ 2015-09-21 16:48  crazyYong  阅读(1152)  评论(0编辑  收藏  举报