标签:
package com.qiezi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.CharSetUtils;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Test;
public class CommonsLangTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String[] test1 = { "ddffd", "33" };
String[] test = { "33", "ddffd" };
/**
* 1:判断连个对象是否相等
*
*/
System.out.println(ArrayUtils.isEquals(test, test1)); // false
/**
* 2.{33,ddffd} 将数组内容以{,}形式输出.
*/
System.out.println(ArrayUtils.toString(test));
/**
* 3.ArraytoMap 将数组装换成map,迭代map
*/
Map<String, String> map = ArrayUtils.toMap(new String[][] {
{ "湖北省省会", "武汉" }, { "河南省省会", "郑州" }, { "广东省省会", "广州" } });
/**
* 方式一 下面是遍历map的方式,取得其keySet.iterator();
*/
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = map.get(key);
System.out.println("key:" + key + "value:" + value);
}
System.out.println("--------------");
Iterator its = map.entrySet().iterator();
while (its.hasNext()) {
Entry entry = (Map.Entry) its.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("key:" + key + "value:" + value);
}
/**
* 4.取得类名、包名
*/
System.out.println(ClassUtils.getShortClassName(CommonsLangTest.class));
System.out.println(ClassUtils.getPackageName(CommonsLangTest.class));
/**
* 5.NumberUtils
*/
System.out.println(NumberUtils.compare(12.3, 632.3));
System.out.println(NumberUtils.isDigits("3213")); // 要求是整数
System.out.println(NumberUtils.isNumber("214.21")); // 可以为数字或者是整数
/**
* 五位的随机字母和数字
*/
System.out.println(RandomStringUtils.randomAlphanumeric(5));
/**
* 生成12为的字符串
*/
System.out.println(RandomStringUtils.random(12));
/**
* 在指定的字符中,生成12为的字符串 acbadccbddcb
*/
System.out.println(RandomStringUtils.random(12, "abcd"));
/**
* 12位的随机字母(包含大小写)
*/
System.out.println(RandomStringUtils.randomAlphabetic(12));
System.out.println("------会生成包含稀有 的字符 ------");
System.out.println(RandomStringUtils.randomAscii(50));
System.out.println(5 | 4);
System.out.println(4 | 3);
/**
* StringEscapeUtils
*/
System.out.println(StringEscapeUtils.escapeHtml("<html>"));
System.out.println(StringEscapeUtils.escapeJava("//<String>")); // \/\/<String>
/**
* 8.StringUtils,判断是否是空格字符 false true true
*/
System.out.println(StringUtils.isBlank(" 33")); // false
System.out.println(StringUtils.isBlank("")); // true
System.out.println(StringUtils.isBlank(" ")); // true
System.out.println(StringUtils.isBlank("\n\n\t")); // true
System.out.println("+++++++++++++++++++++++++++++++++");
/**
* 判断是否为空
*/
System.out.println(StringUtils.isEmpty(" 33")); // false
System.out.println(StringUtils.isEmpty("")); // true
System.out.println(StringUtils.isEmpty(" ")); // false
System.out.println(StringUtils.isEmpty("\n\n\t")); // false
System.out.println(StringUtils.isEmpty(null)); // true
System.out.println("+++++++++++++++++++++++++++++++++");
// 将数组中的内容以,分隔
System.out.println(StringUtils.join(test, ",")); // 33,ddffd
// 表示左边两个字符
System.out.println(StringUtils.left("abc", 2));
System.out.println(StringUtils.right("abcd", 3));
// 在右边加下字符,使之总长度为6
System.out.println(StringUtils.rightPad("abc", 6, ‘T‘));
// 首字母大写 Abc
System.out.println(StringUtils.capitalize("abc"));
// Deletes all whitespaces from a String 删除所有空格 abc
System.out.println(StringUtils.deleteWhitespace(" a b c "));
System.out.println(" a a s ".trim()); // a a s
// 判断是否包含这个字符 前面是被搜索的字符,后面是 是否包含该字符串
System.out.println(StringUtils.contains("asss", "as")); // true
/**
* System.out.println(StringUtils.center("haizai", 0)); Exception in
* thread "main" java.lang.IllegalArgumentException: Minimum
* abbreviation width is 4 获取一个字符串的缩写,第二个参数要大于 3
*
*/
String test2 = "haizainalima";
String test3 = "haizai";
System.out.println(StringUtils.abbreviate(test2, 4)); // h...
System.out.println(StringUtils.abbreviate(test2, 14)); // haizainalima
System.out.println(StringUtils.abbreviate(test3, 4)); // h...
System.out.println("000000000");
/**
* 获取一个字符传中的两个指定字符之间的所有的 字符 ,不存在就返回null,如果两个字符是紧紧挨着的就返回空的 字符串
*
*/
String test4 = "qiezivae";
System.out.println(StringUtils.substringBetween(test4, "qiezi", "e")); // va
System.out.println(StringUtils.substringBetween(test4, "qiezi", "vae"));//
System.out.println(StringUtils.substringBetween(test4, "qiezi", "zi")); // null
System.out.println(StringUtils.isBlank(StringUtils.substringBetween(
test4, "qiezi", "zi"))); // true
/**
* 将指定的字符串重复N次后进行返回 qiezi-vaeqiezi-vaeqiezi-vaeqiezi-vaeqiezi-vae
*/
System.out.println(StringUtils.repeat("qiezi-vae", 5));
System.out.println(StringUtils.remove("qiezivae", ‘v‘)); // qieziae
System.out.println(StringUtils.remove("qiezivae", "vae")); // qiezi
/**
* 将 AAA 插入到将 字符串 BBB重复 多次后的中间,得到的总长度是 N
* qieziqieziqieziqieziqievaeqieziqieziqieziqieziqiez
* 22222222222222222222222vae222222222222222222222222
*/
System.out.println(StringUtils.center("vae", 50, "qiezi"));
System.out.println(StringUtils.center("vae", 50, ‘2‘));
/**
* 翻转一个字符串
*/
System.out.println(StringUtils.reverse("qiezivae"));
System.out.println(StringUtils.reverseDelimited("qiezivae", ‘z‘)); // ivaezqie
// 以z
// 为
// 界限
// 两边的都进行翻转
System.out
.println(StringUtils.reverseDelimitedString("qiezivae", "zi"));// vaezieziq
/**
* 判断字符串的组成
*/
System.out.println(StringUtils.isNumeric("121")); // true
System.out.println(StringUtils.isNumeric("2s"));// false
System.out.println(StringUtils.isAlpha("haizia"));// true
System.out.println(StringUtils.isAlpha("ha45"));// true
System.out.println(StringUtils.isAlphanumeric("haizai45"));// true
System.out.println(StringUtils.isAlphanumeric("haizai"));// true
System.out.println(StringUtils.isAlphanumeric("46566"));// true
System.out.println(StringUtils.isAlphanumericSpace("haizai haizai"));// true
System.out.println(StringUtils.isAlphanumericSpace("haizaihaizai"));// true
System.out.println(StringUtils.isNumericSpace("4554 513"));// true
System.out.println(StringUtils.isNumericSpace("4554513"));// true
/**
* 取得一个字符串在另一个字符转中出现的次数
*/
System.out.println(StringUtils.countMatches("qiezivaeqiezivaeqiezivae",
"vae")); // 3
/**
* 字符串中的字符不可以以指定字符串中的字符开始或者是结束
*/
System.out.println("_____________________________");
System.out.println(StringUtils.strip(" hai zai ")); // hai zai
System.out.println(StringUtils.strip(null)); // null
System.out.println(StringUtils.strip("", "")); //
System.out.println(StringUtils.strip(null, null)); // null
System.out.println(StringUtils.strip("qieziva te", "qiezi")); // va t
System.out.println(StringUtils.stripStart("qieziva te", "qiezi")); // va
// te
System.out.println(StringUtils.strip("qieziva te", "e")); // qieziva t
System.out.println(StringUtils.replace("qie zi vae", "vae", "vat")); // qie
// zi
// vat
System.out.println(StringUtils.replace("qie zi vae", " ", "")); // qiezivae
System.out.println(StringUtils.containsIgnoreCase("qiezivae", "vae"));
/**
* 判断一个字符串是否只包含有指定数组中的字符
*/
System.out.println(StringUtils.containsOnly(" ", " ")); // true
System.out.println(StringUtils.containsOnly(null, "ss")); // false
System.out.println(StringUtils.containsOnly("haizai", "ahiz"));// true
System.out.println(StringUtils.containsOnly("haizpai", "ahiz"));// false
System.out.println(StringUtils.containsOnly("ass as", new char[] { ‘a‘,
‘s‘, ‘ ‘ })); // true
System.out.println(StringUtils.containsOnly("as saos", new char[] {
‘a‘, ‘f‘, ‘s‘, ‘ ‘ }));// false
System.out
.println("====================================================");
System.out.println("计算字符串中包含某字符数.");
System.out.println(CharSetUtils.count(
"The quick brown fox jumps over the lazy dog.", "aeiou"));
System.out.println("删除字符串中某字符.");
System.out.println(CharSetUtils.delete(
"The quick brown fox jumps over the lazy dog.", "aeiou"));
System.out.println("保留字符串中某字符.");
System.out.println(CharSetUtils.keep(
"The quick brown fox jumps over the lazy dog.", "aeiou"));
System.out.println("合并重复的字符.");
System.out.println(CharSetUtils.squeeze("a bbbbbb c dd", "b d"));
System.out.println("Object为null时,默认打印某字符.");
Object obj = null;
System.out.println(ObjectUtils.defaultIfNull(obj, "空"));
System.out.println("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");
Object a = new Object();
Object b = a;
Object c = new Object();
System.out.println(ObjectUtils.equals(a, b)); // true
System.out.println(ObjectUtils.equals(a, c)); // false
System.out.println("用父类Object的toString()方法返回对象信息.");
Date date = new Date();
System.out.println(ObjectUtils.identityToString(date)); //
System.out.println(Date.class); // class java.util.Date
System.out.println(date);// Thu Feb 13 19:27:34 CST 2014
System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");
System.out.println(ObjectUtils.toString(date)); // Thu Feb 13 19:29:28
// CST 2014
System.out.println(ObjectUtils.toString(null));// 就是输出一个空的换行
System.out.println(date); // Thu Feb 13 19:29:28 CST 2014
System.out.println("--------------------------------------");
System.out.println("*SerializationUtils**");
byte[] bytes = SerializationUtils.serialize(date);
/**
* {-84,-19,0,5,115,114,0,14,106,97,118,97,46,117,116,105,108,46,68,97,
* 116
* ,101,104,106,-127,1,75,89,116,25,3,0,0,120,112,119,8,0,0,1,68,43,4,
* -91,-67,120}
*/
System.out.println(ArrayUtils.toString(bytes));
// Thu Feb 13 19:32:08 CST 2014
System.out.println(date);
Date reDate = (Date) SerializationUtils.deserialize(bytes);
System.out.println(reDate); // Thu Feb 13 19:33:44 CST 2014
System.out.println(ObjectUtils.equals(date, reDate)); // true
System.out.println(date == reDate); // false 要注意的是这里的 equels 方法比较的结果是
// true ,但是 内存地址已经不一样了。
System.out.println("返回两字符串不同处索引号.");
System.out.println(StringUtils.indexOfDifference("aaabc", "aaacc")); // 3下标是从零开始的。
System.out.println("返回两字符串不同处开始至结束.");
System.out.println(StringUtils.difference("aaabcde", "aaaccde")); // ccde
System.out.println("截去字符串为以指定字符串结尾的部分.");
System.out.println(StringUtils.chomp("aaabcde", "de")); // aaabc
System.out.println("检查一字符串是否为另一字符串的子集.");
System.out.println(StringUtils.containsOnly("aad", "aadd")); // true
System.out.println("检查一字符串是否不是另一字符串的子集.");
System.out.println(StringUtils.containsNone("defg", "aadd")); // false
System.out.println("检查一字符串是否包含另一字符串.");
System.out.println(StringUtils.contains("defg", "ef")); // true
System.out.println(StringUtils.containsOnly("ef", "defg"));// true
// 前面的字符串是由后面的字符组成的,返回true
System.out.println("返回可以处理null的toString().");
System.out.println(StringUtils.defaultString("aaaa")); // aaaa
System.out.println("?" + StringUtils.defaultString(null) + "!");// ?!
String[] strArray = StringUtils.split("a,b,,c,d,null,e", ",");
System.out.println(strArray.length); // 6
System.out.println(strArray.toString());// [Ljava.lang.String;@192b996
// a=b=c=d=null=e= 也就是说 ‘‘ 直接的会被直接的省去
for (int i = 0; i < strArray.length; i++) {
System.out.print(strArray[i] + "=");
}
System.out.println("获得系统文件分隔符.");
System.out.println(SystemUtils.FILE_SEPARATOR);// \
System.out.println("获得源文件编码.");
System.out.println(SystemUtils.FILE_ENCODING); // GBK
System.out.println("获得ext目录.");
// D:\myeclipse8.6\comm\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
System.out.println(SystemUtils.JAVA_EXT_DIRS);
System.out.println("获得java版本.");
System.out.println(SystemUtils.JAVA_VM_VERSION); // 11.3-b02
System.out.println("获得java厂商.");
System.out.println(SystemUtils.JAVA_VENDOR);// Sun Microsystems Inc.
}
@Test
public void SerializationUtilsTest() {
Date date = new Date();
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream(new File("e:/sss.txt"));
fis = new FileInputStream(new File("e:/sss.txt"));
SerializationUtils.serialize(date, fos);
Date reDate2 = (Date) SerializationUtils.deserialize(fis);
System.out.println(date.equals(reDate2)); // true
} catch (FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void classUtilsDemo() {
System.out.println("获取类实现的所有接口.");
// [interface java.io.Serializable, interface java.lang.Cloneable,
// interface java.lang.Comparable]
System.out.println(ClassUtils.getAllInterfaces(Date.class));
System.out.println("获取类所有父类.");
// [class java.lang.Object]
System.out.println(ClassUtils.getAllSuperclasses(Date.class));
System.out.println("获取简单类名.");
// Date
System.out.println(ClassUtils.getShortClassName(Date.class));
System.out.println("获取包名.");
// java.util
System.out.println(ClassUtils.getPackageName(Date.class));
System.out.println("判断是否可以转型.");
// true(前者向后者进行转化)
System.out.println(ClassUtils.isAssignable(Date.class, Object.class));
// false (前者向后者进行转化)
System.out.println(ClassUtils.isAssignable(Object.class, Date.class));
}
@Test
public void numberUtils() {
System.out.println("字符串转为数字(不知道有什么用).");
// 33
System.out.println(NumberUtils.toInt("ba", 33));
System.out.println("从数组中选出最大值.");
// 4
System.out.println(NumberUtils.max(new int[] { 1, 2, 3, 4 }));
System.out.println("判断字符串是否全是整数.");
// flase
System.out.println(NumberUtils.isDigits("123.1"));
System.out.println("判断字符串是否是有效数字.");
// true
System.out.println(NumberUtils.isNumber("0123.1"));
System.out.println(DateFormatUtils.format(System.currentTimeMillis(),
"yyyy-MM-dd HH:mm:ss"));
/**
* 14-02-10 00:00 14-02-11 00:00 14-02-12 00:00 14-02-13 00:00 14-02-14
* 00:00 14-02-15 00:00 14-02-16 00:00
*/
for (Iterator iterator = DateUtils.iterator(new Date(),
DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {
Calendar cal = (Calendar) iterator.next();
System.out.println(DateFormatUtils.format(cal.getTime(),
"yy-MM-dd HH:mm"));
}
}
@Test
public void genHeader() {
/**
* **************************************************
* ^O^^O^^O^^O^^O^^O^^O haizai ^O^^O^^O^^O^^O^^O^^O
**************************************************
*/
String head = "haizai";
String[] header = new String[3];
header[0] = StringUtils.repeat("*", 50);
header[1] = StringUtils.center(" " + head + " ", 50, "^O^");
header[2] = header[0];
System.out.println(StringUtils.join(header, "\n"));
}
@Test
public void validateDemo() {
String[] strarray = { "a", "b", "c" };
System.out.println("验证功能");
// 没有返回值 有什么用呢??
Validate.notEmpty(strarray);
}
@Test
public void wordUtilsDemo() {
System.out.println("单词处理功能");
String str1 = "wOrD";
String str2 = "ghj\nui\tpo";
System.out.println(WordUtils.capitalize(str1)); // 首字母大写 // WOrD
System.out.println(WordUtils.capitalizeFully(str1)); // 首字母大写其它字母小写 Word
char[] ctrg = { ‘.‘ };
System.out.println(WordUtils.capitalizeFully("i aM.fine", ctrg)); // 在规则地方转换 I am.Fine
System.out.println(WordUtils.initials(str1)); // 获取首字母// w
System.out.println(WordUtils.initials("Ben John Lee", null)); // 取每个单词的首字母 BJL
char[] ctr = { ‘ ‘, ‘.‘ };
System.out.println(WordUtils.initials("Ben J.Lee", ctr)); // 按指定规则获取首字母 //BJL
System.out.println(WordUtils.swapCase(str1)); // 大小写逆转 // WoRd
/**
* ghj
ui po
*/
System.out.println(WordUtils.wrap(str2, 1)); // 解析\n和\t等字符
}
}
001 public class TestLangDemo {
002
003 public void charSetDemo() {
004 System.out.println("**CharSetDemo**");
005 CharSet charSet = CharSet.getInstance("aeiou");
006 String demoStr = "The quick brown fox jumps over the lazy dog.";
007 int count = 0;
008 for (int i = 0, len = demoStr.length(); i < len; i++) {
009 if (charSet.contains(demoStr.charAt(i))) {
010 count++;
011 }
012 }
013 System.out.println("count: " + count);
014 }
015
016 public void charSetUtilsDemo() {
017 System.out.println("**CharSetUtilsDemo**");
018 System.out.println("计算字符串中包含某字符数.");
019 System.out.println(CharSetUtils.count("The quick brown fox jumps over the lazy dog.", "aeiou"));
020
021 System.out.println("删除字符串中某字符.");
022 System.out.println(CharSetUtils.delete("The quick brown fox jumps over the lazy dog.", "aeiou"));
023
024 System.out.println("保留字符串中某字符.");
025 System.out.println(CharSetUtils.keep("The quick brown fox jumps over the lazy dog.", "aeiou"));
026
027 System.out.println("合并重复的字符.");
028 System.out.println(CharSetUtils.squeeze("a bbbbbb c dd", "b d"));
029 }
030
031 public void objectUtilsDemo() {
032 System.out.println("**ObjectUtilsDemo**");
033 System.out.println("Object为null时,默认打印某字符.");
034 Object obj = null;
035 System.out.println(ObjectUtils.defaultIfNull(obj, "空"));
036
037 System.out.println("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");
038 Object a = new Object();
039 Object b = a;
040 Object c = new Object();
041 System.out.println(ObjectUtils.equals(a, b));
042 System.out.println(ObjectUtils.equals(a, c));
043
044 System.out.println("用父类Object的toString()方法返回对象信息.");
045 Date date = new Date();
046 System.out.println(ObjectUtils.identityToString(date));
047 System.out.println(date);
048
049 System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");
050 System.out.println(ObjectUtils.toString(date));
051 System.out.println(ObjectUtils.toString(null));
052 System.out.println(date);
053 }
054
055 public void serializationUtilsDemo() {
056 System.out.println("*SerializationUtils**");
057 Date date = new Date();
058 byte[] bytes = SerializationUtils.serialize(date);
059 System.out.println(ArrayUtils.toString(bytes));
060 System.out.println(date);
061
062 Date reDate = (Date) SerializationUtils.deserialize(bytes);
063 System.out.println(reDate);
064 System.out.println(ObjectUtils.equals(date, reDate));
065 System.out.println(date == reDate);
066
067 FileOutputStream fos = null;
068 FileInputStream fis = null;
069 try {
070 fos = new FileOutputStream(new File("d:/test.txt"));
071 fis = new FileInputStream(new File("d:/test.txt"));
072 SerializationUtils.serialize(date, fos);
073 Date reDate2 = (Date) SerializationUtils.deserialize(fis);
074
075 System.out.println(date.equals(reDate2));
076
077 } catch (FileNotFoundException e) {
078 e.printStackTrace();
079 } finally {
080 try {
081 fos.close();
082 fis.close();
083 } catch (IOException e) {
084 e.printStackTrace();
085 }
086 }
087
088 }
089
090 public void randomStringUtilsDemo() {
091 System.out.println("**RandomStringUtilsDemo**");
092 System.out.println("生成指定长度的随机字符串,好像没什么用.");
093 System.out.println(RandomStringUtils.random(500));
094
095 System.out.println("在指定字符串中生成长度为n的随机字符串.");
096 System.out.println(RandomStringUtils.random(5, "abcdefghijk"));
097
098 System.out.println("指定从字符或数字中生成随机字符串.");
099 System.out.println(RandomStringUtils.random(5, true, false));
100 System.out.println(RandomStringUtils.random(5, false, true));
101
102 }
103
104 public void stringUtilsDemo() {
105 System.out.println("**StringUtilsDemo**");
106 System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
107 String[] header = new String[3];
108 header[0] = StringUtils.repeat("*", 50);
109 header[1] = StringUtils.center(" StringUtilsDemo ", 50, "^O^");
110 header[2] = header[0];
111 String head = StringUtils.join(header, "\n");
112 System.out.println(head);
113
114 System.out.println("缩短到某长度,用...结尾.");
115 System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10));
116 System.out.println(StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 15, 10));
117
118 System.out.println("返回两字符串不同处索引号.");
119 System.out.println(StringUtils.indexOfDifference("aaabc", "aaacc"));
120
121 System.out.println("返回两字符串不同处开始至结束.");
122 System.out.println(StringUtils.difference("aaabcde", "aaaccde"));
123
124 System.out.println("截去字符串为以指定字符串结尾的部分.");
125 System.out.println(StringUtils.chomp("aaabcde", "de"));
126
127 System.out.println("检查一字符串是否为另一字符串的子集.");
128 System.out.println(StringUtils.containsOnly("aad", "aadd"));
129
130 System.out.println("检查一字符串是否不是另一字符串的子集.");
131 System.out.println(StringUtils.containsNone("defg", "aadd"));
132
133 System.out.println("检查一字符串是否包含另一字符串.");
134 System.out.println(StringUtils.contains("defg", "ef"));
135 System.out.println(StringUtils.containsOnly("ef", "defg"));
136
137 System.out.println("返回可以处理null的toString().");
138 System.out.println(StringUtils.defaultString("aaaa"));
139 System.out.println("?" + StringUtils.defaultString(null) + "!");
140
141 System.out.println("去除字符中的空格.");
142 System.out.println(StringUtils.deleteWhitespace("aa bb cc"));
143
144 System.out.println("分隔符处理成数组.");
145 String[] strArray = StringUtils.split("a,b,,c,d,null,e", ",");
146 System.out.println(strArray.length);
147 System.out.println(strArray.toString());
148
149 System.out.println("判断是否是某类字符.");
150 System.out.println(StringUtils.isAlpha("ab"));
151 System.out.println(StringUtils.isAlphanumeric("12"));
152 System.out.println(StringUtils.isBlank(""));
153 System.out.println(StringUtils.isNumeric("123"));
154 }
155
156 public void systemUtilsDemo() {
157 System.out.println(genHeader("SystemUtilsDemo"));
158 System.out.println("获得系统文件分隔符.");
159 System.out.println(SystemUtils.FILE_SEPARATOR);
160
161 System.out.println("获得源文件编码.");
162 System.out.println(SystemUtils.FILE_ENCODING);
163
164 System.out.println("获得ext目录.");
165 System.out.println(SystemUtils.JAVA_EXT_DIRS);
166
167 System.out.println("获得java版本.");
168 System.out.println(SystemUtils.JAVA_VM_VERSION);
169
170 System.out.println("获得java厂商.");
171 System.out.println(SystemUtils.JAVA_VENDOR);
172 }
173
174 public void classUtilsDemo() {
175 System.out.println(genHeader("ClassUtilsDemo"));
176 System.out.println("获取类实现的所有接口.");
177 System.out.println(ClassUtils.getAllInterfaces(Date.class));
178
179 System.out.println("获取类所有父类.");
180 System.out.println(ClassUtils.getAllSuperclasses(Date.class));
181
182 System.out.println("获取简单类名.");
183 System.out.println(ClassUtils.getShortClassName(Date.class));
184
185 System.out.println("获取包名.");
186 System.out.println(ClassUtils.getPackageName(Date.class));
187
188 System.out.println("判断是否可以转型.");
189 System.out.println(ClassUtils.isAssignable(Date.class, Object.class));
190 System.out.println(ClassUtils.isAssignable(Object.class, Date.class));
191 }
192
193 public void stringEscapeUtilsDemo() {
194 System.out.println(genHeader("StringEcsapeUtils"));
195 System.out.println("转换特殊字符.");
196 System.out.println("html:" + StringEscapeUtils.escapeHtml3(" "));
197 System.out.println("html:" + StringEscapeUtils.escapeHtml4(" "));
198 System.out.println("html:" + StringEscapeUtils.unescapeHtml3("<p>"));
199 System.out.println("html:" + StringEscapeUtils.unescapeHtml4("<p>"));
200 }
201
202 private final class BuildDemo {
203 String name;
204 int age;
205
206 public BuildDemo(String name, int age) {
207 this.name = name;
208 this.age = age;
209 }
210
211 public String toString() {
212 ToStringBuilder tsb = newToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
213 tsb.append("Name", name);
214 tsb.append("Age", age);
215 return tsb.toString();
216 }
217
218 public int hashCode() {
219 HashCodeBuilder hcb = new HashCodeBuilder();
220 hcb.append(name);
221 hcb.append(age);
222 return hcb.hashCode();
223 }
224
225 public boolean equals(Object obj) {
226 if (!(obj instanceof BuildDemo)) {
227 return false;
228 }
229 BuildDemo bd = (BuildDemo) obj;
230 EqualsBuilder eb = new EqualsBuilder();
231 eb.append(name, bd.name);
232 eb.append(age, bd.age);
233 return eb.isEquals();
234 }
235 }
236
237 public void builderDemo() {
238 System.out.println(genHeader("BuilderDemo"));
239 BuildDemo obj1 = new BuildDemo("a", 1);
240 BuildDemo obj2 = new BuildDemo("b", 2);
241 BuildDemo obj3 = new BuildDemo("a", 1);
242
243 System.out.println("toString()");
244 System.out.println(obj1);
245 System.out.println(obj2);
246 System.out.println(obj3);
247
248 System.out.println("hashCode()");
249 System.out.println(obj1.hashCode());
250 System.out.println(obj2.hashCode());
251 System.out.println(obj3.hashCode());
252
253 System.out.println("equals()");
254 System.out.println(obj1.equals(obj2));
255 System.out.println(obj1.equals(obj3));
256 }
257
258 public void numberUtils() {
259 System.out.println(genHeader("NumberUtils"));
260 System.out.println("字符串转为数字(不知道有什么用).");
261 System.out.println(NumberUtils.toInt("ba", 33));
262
263 System.out.println("从数组中选出最大值.");
264 System.out.println(NumberUtils.max(new int[] { 1, 2, 3, 4 }));
265
266 System.out.println("判断字符串是否全是整数.");
267 System.out.println(NumberUtils.isDigits("123.1"));
268
269 System.out.println("判断字符串是否是有效数字.");
270 System.out.println(NumberUtils.isNumber("0123.1"));
271 }
272
273 public void dateFormatUtilsDemo() {
274 System.out.println(genHeader("DateFormatUtilsDemo"));
275 System.out.println("格式化日期输出.");
276 System.out.println(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
277
278 System.out.println("秒表.");
279 StopWatch sw = new StopWatch();
280 sw.start();
281
282 for (Iterator iterator = DateUtils.iterator(newDate(), DateUtils.RANGE_WEEK_CENTER); iterator.hasNext();) {
283 Calendar cal = (Calendar) iterator.next();
284 System.out.println(DateFormatUtils.format(cal.getTime(), "yy-MM-dd HH:mm"));
285 }
286
287 sw.stop();
288 System.out.println("秒表计时:" + sw.getTime());
289
290 }
291
292 private String genHeader(String head) {
293 String[] header = new String[3];
294 header[0] = StringUtils.repeat("*", 50);
295 header[1] = StringUtils.center(" " + head + " ", 50, "^O^");
296 header[2] = header[0];
297 return StringUtils.join(header, "\n");
298 }
299
300 private void validateDemo() {
301 String[] strarray = { "a", "b", "c" };
302 System.out.println("验证功能");
303 System.out.println(Validate.notEmpty(strarray));
304 }
305
306 private void wordUtilsDemo() {
307 System.out.println("单词处理功能");
308 String str1 = "wOrD";
309 String str2 = "ghj\nui\tpo";
310 System.out.println(WordUtils.capitalize(str1)); // 首字母大写
311 System.out.println(WordUtils.capitalizeFully(str1)); // 首字母大写其它字母小写
312 char[] ctrg = { ‘.‘ };
313 System.out.println(WordUtils.capitalizeFully("i aM.fine", ctrg)); // 在规则地方转换
314 System.out.println(WordUtils.initials(str1)); // 获取首字母
315 System.out.println(WordUtils.initials("Ben John Lee", null)); // 取每个单词的首字母
316 char[] ctr = { ‘ ‘, ‘.‘ };
317 System.out.println(WordUtils.initials("Ben J.Lee", ctr)); // 按指定规则获取首字母
318 System.out.println(WordUtils.swapCase(str1)); // 大小写逆转
319 System.out.println(WordUtils.wrap(str2, 1)); // 解析\n和\t等字符
320 }
321
322 /**
323 * @param args
324 */
325 public static void main(String[] args) {
326 TestLangDemo langDemo = new TestLangDemo();
327
328 langDemo.charSetDemo();
329 langDemo.charSetUtilsDemo();
330 langDemo.objectUtilsDemo();
331 langDemo.serializationUtilsDemo();
332 langDemo.randomStringUtilsDemo();
333 langDemo.stringUtilsDemo();
334 langDemo.systemUtilsDemo();
335 langDemo.classUtilsDemo();
336 langDemo.stringEscapeUtilsDemo();
337 langDemo.builderDemo();
338 langDemo.numberUtils();
339 langDemo.dateFormatUtilsDemo();
340 langDemo.validateDemo();
341 langDemo.wordUtilsDemo();
342 }
343
344 }
标签:
原文地址:http://www.cnblogs.com/safely-pointer/p/4645067.html