apache常用的工具类,apache常用工具类


1.判断一个list是否为空

List<Item> itemList = itemService.listByOrderIdList(orderIdList);
CollectionUtils.isNotEmpty(itemList)
2.获取不同精度的时间,参数还可以为:Calendar.HOUR_OF_DAY,Calendar.MINUTE,Calendar.SECOND等
Date itemDate = null;
itemDate = DateUtils.truncate(item.getUpdateTime(), Calendar.DATE);
3.从一个entity中把属性复制进另外一个entity中
StuForm form = new StuForm("lee", 18, 1, new Date(), true);
Stu stu = new Stu(); 
BeanUtils.copyProperties(form, stu);
System.out.println(stu.toString());
4.文件操作工具类
//1.读取Stream 
//标准代码:  
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();  
try {  
     InputStreamReader inR = new InputStreamReader( in );  
     BufferedReader buf = new BufferedReader( inR );  
     String line;  
     while ( ( line = buf.readLine() ) != null ) {  
         System.out.println( line );  
     }  
} finally {  
     in.close();  
}  
      
//使用IOUtils    
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();  
try {  
    System.out.println( IOUtils.toString( in ) );  
} finally {  
    IOUtils.closeQuietly(in);  
}  
//2.读取文件  
File file = new File("/commons/io/project.properties");  
List lines = FileUtils.readLines(file, "UTF-8");  
//3.查看剩余空间  
long freeSpace = FileSystemUtils.freeSpace("C:/");
5.首字母大写
String str1 = "person";
str1 = StringUtils.capitalize(str1);
System.out.println(str1);   //Person
6.取得字符串的缩进
使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串.
例程:

    String test = "This is a test of the abbreviation.";
    String test2 = "Test";

    System.out.println( StringUtils.abbreviate( test, 15 ) );
    System.out.println( StringUtils.abbreviate( test, 5,15 ) );
    System.out.println( StringUtils.abbreviate( test2, 10 ) );
输出如下:
This is a te...
...is a test...
Test
7.取得某字符串在另一字符串中出现的次数
使用函数:StringUtils.countMatches(testString,seqString)
函数介绍:取得seqString在testString中出现的次数,未发现则返回零
例程:
System.out.println(StringUtils.countMatches( "Chinese People", "e"));
输出:4
8.是否都是由字母组成
 public static boolean isAlpha(String str);  只由字母组成
 public static boolean isAlphaSpace(String str); 只有字母和空格组成
 public static boolean isAlphanumeric(String str);只由字母和数字组成
 public static boolean isAlphanumericSpace(String str);只由字母数字和空格组成
 public static boolean isNumeric(String str);只由数字组成
 public static boolean isNumericSpace(String str);只由数字和空格组成


相关内容

    暂无相关文章