阿帕奇常用函数总结Word文档格式.docx
《阿帕奇常用函数总结Word文档格式.docx》由会员分享,可在线阅读,更多相关《阿帕奇常用函数总结Word文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
利用JDK中String类的trim()方法。
6publicstaticStringstripStart(Stringstr,StringstripChars);
去掉str前端的在stripChars中的字符
7publicstaticStringstripEnd(Stringstr,StringstripChars);
去掉str末端的在stripChars中的字符
8publicstaticintordinalIndexOf(Stringstr,StringsearchStr,intordinal)
返回字符串search在字符串str中第ordinal次出现的位置。
如果str=null或searchStr=null或ordinal<
=0则返回-1.
StringEscapeUtils这个类里提供了很多转义的方法,比如可以转成json、xml、html等格式。
1publicstaticStringescapeJava(Stringstr);
使用javaStringrules转义给定字符串str.
正确处理引号和控制字符(如,tab,backslash,cr,ff等).
因此,一个tab符号转义成’\\t’.
2publicstaticStringescapeJavaScript(Stringstr)
同方法1.唯一区别是,在javaScript中,单引号必须被转义。
1.final变量
EMPTY_*_ARRAY:
根据*处的类型,返回对应的长度为0的数组。
注意原始变量及其包裹类。
INDEX_NOT_FOUND:
返回-1,表述数组下标未找到。
2.toString(Object,[,stringIfNull]):
如果为空,返回stringIfnULL
3.hashCode(Object):
使用HashCodeBuilder返回数组的hashcode
4.isEquals(Object1,Object2):
用EqualsBuilder返回两个数组比较的结果。
5.toMap(Object[]):
作为参数的数组有两个选择,一是成员为Map.Entry,然后通过遍历该数组,把Map.Entry拆分并分别放入新生成Map的Key和Value中;
二是成员为长度大于等于2的数组,位置0的元素作为key,位置1的元素作为value。
除了以上两种情况,会抛出异常。
6.clone族
Object[]:
如果不为空,则使用参数自己的clone方法处理。
Long[],int[],short[],char[],byte[],double[],float[],Boolean[]同上。
7.subarray族
Object[],start,end:
Object[],start,end:
如果不为空,start小于0,取0;
end大于长度,取长度。
如果start大于end,则用Array.newInstance返回一个空的对象数组,其类型由参数的getClass().getComponentType()决定。
否则同样定义一个长度为end-start的数组,使用System.arraycopy进行数组的拷贝。
8.isSameLength族
略.
9.isSameType
Object1,Object2:
如果一方为空,抛出IllegalArgumentException.否则根据getClass().getName()比较两个参数的类名是否相同.
10.reverse族
从数组前后两个坐标i,j开始,交换数据,并向中间移动。
当i>
j时停止。
11.下标查找族
11.1indexOf
Object[],object[,startIndex]:
Object[]为空,则返回-1,。
如果startIndex小于0,则置为0.如果object为空,则从数组的第一个位置开始找第一个为空的数据。
否则,利用Object.equals()方法正向遍历数组,获取匹配的下标并返回。
否则返回-1.
1staticfinal
MILLIS_PER_SECOND=1000;
一秒等于1000毫秒
MILLIS_PER_MINUTE=60*MILLIS_PER_SECOND;
一分钟等于多少毫秒
MILLIS_PER_HOUR=60*MILLIS_PER_MINUTE;
一小时等于多少毫秒
MILLIS_PER_DAY=24*MILLIS_PER_HOUR;
一天等于多少毫秒
2日期比较
publicstaticbooleanisSameDay(Datedate1,Datedate2);
publicstaticbooleanisSameDay(Calendarcal1,Calendarcal2);
比较日期是否相同,忽略time
具体是通过比较Calendar.ERA、YEAR、DAY_OF_YEAR三个属性判断给定日期是否相同.
3时间比较
publicstaticbooleanisSameInstant(Datedate1,Datedate2);
publicstaticbooleanisSameInstant(Calendarcal1,Calendarcal2);
比较时间是否相同.
具体是通过Date类中的getTime()方法的。
4ADD族
publicstaticDateaddYears(Datedate,intamount);
在给定日期date的基础上添加amount年,注意,函数返回新的对象;
以下同上。
addMonths();
//月
addWeeks();
//周
addDays();
//日
addHours();
//小时
addMinutes();
//分钟
addSeconds();
//秒
addMilliseconds();
//毫秒
5set族
publicstaticDatesetYears(Datedate,intamount);
为date设置新的年份信息,并返回一个新的对象,对象date未发生改变.
以下方法同上。
setMonths();
//设置月份
setDays();
//设置日期
setHours();
//设置小时
setMinutes();
//设置分钟
setSeconds();
//设置秒
setMilliseconds();
//设置毫秒
6round族、truncate族、ceil族
日期取整(日期精度调节,如调节至秒/分等)
Dateround(Datedate,intfield);
DateUtils.round()相当于数学中的四舍五入法取整
Datetruncate(Datedate,intfield);
DateUtils.truncate()相当与去余法取整。
Dateceiling(Datedate,intfield);
DateUtils.ceiling()相当于向上取整。
能对Calendar类中几乎所有的field做日期取整,包括Calender.YEAR,Calendar.SECOND,Calendar.MINUTE,Calendar.HOUR,
Calendar.DAY_OF_MONTH,Calendar.MONTH.
与SUN的SimpleDateFormat相比,其主要优点是:
线程安全。
对应于SimpleDateFormat的format()的方法,是DateFormatUtils的format系列方法,常用的就是:
publicstaticjava.lang.Stringformat(java.util.Datedate,java.lang.Stringpattern);
DateFormatUtils定义了很多内置的固定日期格式,均为FastDateFormat类型,比如ISO_DATE_FORMAT。
使用FastDateFormat的format()方法可以直接将日期格式化为内置的固定格式。
publicjava.lang.Stringformat(java.util.Datedate)
随机数据生成类,包括浮点,双精,布尔,整形,长整在内的随机数生成
RandomUtils.nextInt();
采用默认的JVMRandom类,数值范围0~2147483647
nextInt(Randomrandom);
也可以设置其他的random
还支持以下方法:
nextLong();
nextBoolean();
nextFloat();
nextDouble();
为JDK中的Number类提供额外的功能。
提供可复用的值为0,1的数值型的包装类。
包括Long、Integer、Short、Byte、Double、Float。
toXXX(Stringstr[,XXX])族
将给定的字符串装换成XXX代表的数值类型,包括Long\Integer\Short\Byte\Double\Float。
如果指定了默认值XXX,那么当String为空,或是转换发生异常,则返回指定的默认值XXX。
createXXX(String)族
用给定的字符串创建XXX数值类型的对象。
包括Float\Double\Integer\Long\BigInteger\BigDecimal
XXXmin(XXX[])族
从类型为XXX的数组中找出最小的。
XXX可为long\float\double\byte\short\int
时间复杂度是O(n).
XXXmax(XXX[])族
同min族。
通过反射技术来操作成员变量。
1getField族
FieldgetField(Class<
?
>
String[,Boolean]);
FieldgetDeclaredField(Class<
String[,Boolean]);
getField:
GetsanaccessibleFieldbynamebreakingscopeifrequested.当前类的接口和和父类都会被考虑。
getDeclaredField:
anaccessibleFieldbynamerespectingscope.
仅当前类被考虑。
2readField族
readStaticField(Field[,boolean]);
readStaticField(Class<
String[,boolean]);
readDeclaredStaticField(Class<
readField(Field,Object[,boolean]);
readField(Object,String[,boolean]);
readDeclaredField(Object,String[,boolean])
获取字段的值。
区别是:
带有Declared的仅考虑当前类,其他情况会考虑当前类实现的接口以及其父类。
3writeField族
writeStaticField(Field,Object[,boolean]);
writeStaticField(Class<
String,Object[,boolean]);
writeDeclaredStaticField(Class<
writeField(Field,Object,Object[,boolean]);
writeField(Object,String,Object[,boolean]);
writeDeclaredField(Object,String,Object[,boolean]);
设置字段的值.
4.1.8CharUtils
静态类,不需要创建
1publicstaticbooleanisAscii(charch)
用途:
判断是否为ascii字符
实现:
publicstaticBooleanisAscii(charch){
returnch<
128;
}
2publicstaticbooleanisAsciiAlpha(charch)
判断是否为ascii字母,即值在65到90或者97到122
publicstaticBooleanisAsciiAlpha(charch){
return(ch>
=‘A’&
&
ch<
=‘Z’)||(ch>
=‘a’&
CH<
=‘z’)
3publicstaticBooleanisAsciiAlphaLower(charch)
同2.判断范围是65到90,即a到z.
4publicstaticbooleanisAsciiAlphaUpper(charch);
同2.判断范围是97到122.即A到Z.
5publicstaticbooleanisAsciiAlphanumeric(charch)
判断是否为ascii字符数字,即值在48到57,65到90或者97到122.
PublicstaticbooleanisAsciiAlphanumeric(charch){
cn<
=‘z’)||(ch>
=‘0’&
=‘9’)
6publicstaticBooleanisAsciiControl(charch)
判断是否为控制字符
publicstaticbooleanisAsciiControl(charch){
returnch<
32||ch=127;
7publicstaticbooleanisAsciiNumeric(charch)
判断是否为数字
PublicstaticBooleanisAsciiNumeric(charch){
Returnch>
=‘0’&
=‘9’;
8publicstaticBooleanisAsciiPrintable(charch)
判断是否可打印出得ascii字符
PublicstaticBooleanisAsciiPrintable(charch){
returnch>
=32&
127;
9publicstaticinttoIntValue(charch)
数字转换
PublicstaticinttoIntValue(charch){
if(isAsciiNumeric(ch)==false){
thrownewIllegalArgumentException(“thecharacter”+ch+“isnotintherange‘0’-‘9’”)
Returnch–48;
10publicstaticStringunicodeEscaped(charch)
将ch转换为unicode表示的字符串形式
publicstaticStringunicodeEscaped(charch){
if(ch<
0x10){
return“\\u000”+Integer.toHexString(ch);
}elseif(ch<
0x100){
retrun“\\u00”+Integer.toHexString(ch);
0x1000){
Return“\\u0”+Integer.toHexString(ch);
}
Return“\\u”+Integer.toHexString(ch);
4.1.10BooleanUtils
1negate(Booleanbool)
用法:
否定指定的boolean值
2isTrue(Booleanbool)
检查一个boolean值是否为true,如果参数为null,返回false
3isNotTrue(Booleanbool)
检查一个boolean值是否为false,如果参数为null,返回true
4isFalse(Booleanbool)
检查一个boolean值是否为false,如果是返回true.
如果检查的值为true或null返回false.
5isNotFalse(Booleanbool)
检查一个boolean值是否不为false,如果是返回true
6toBoolean(Booleanbool)
转换一个为null的boolean值,返回一个false.
*<
pre>
*BooleanUtils.toBoolean(Boolean.TRUE)=true
*BooleanUtils.toBoolean(Boolean.FALSE)=false
*BooleanUtils.toBoolean(null)=false
/pre>
7toBooleanDefaultIfNull(Booleanbool,booleanvalueIfNull)
转换一个为null的boolean值,返回后面参数给定的boolean值.
*BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE,false)=true
*BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE,true)=false
*BooleanUtils.toBooleanDefaultIfNull(null,true)=true
*/
8toBoolean(intvalue)
当参数为0是返回false,其它都返回true.
*BooleanUtils.toBoolean(0)=false
*BooleanUtils.toBoolean
(1)=true
*BooleanUtils.toBoolean
(2)=true
9toBooleanObject(intvalue)
当参数为0是返回Boolean.FALSE对象,其它都返回Boolean.TRUE.
*BooleanUtils.toBoolean(0)=Boolean.FALSE
*BooleanUtils.toBoolean
(1)=Boolean.TRUE
*BooleanUtils.toBoolean
(2)=Boolean.TRUE
10toBooleanObject(Integervalue)
当参数为0是返回Boolean.FALSE对象,为null返回null
*其它则返回Boolean.TRUE.
*BooleanUtils.toBoolean(newInteger(0))=Boolean.FALSE
*BooleanUtils.toBoolean(newInteger
(1))=Boolean.TRUE
*BooleanUtils.toBoolean(newInteger(null))=null
11toBoolean(intvalue,inttrueValue,intfalseValue)
*如果第一个参数和第二个参数相等返回true,
*如果第一个参数和第三个参数相等返回false,
*如果都没有相等的,返回一个IllegalArgumentException
**<
*BooleanUtils.toBoolean(0,1,0)=false
*BooleanUtils.toBoolean(1,1,0)=true
*BooleanUtils.toBoolean(2,1,2)=false
*BooleanUtils.toBoolean(2,2,0)=true
/pr