/**
* charset, charsetUtils
*
*/
@SuppressWarnings({ "unused", "deprecation" })
private static void testChar() {
System.out.println(StringUtils.center("CharsetUtils", 40, "="));
CharSet charSet = CharSet.getInstance("aeiou");
Boolean isTrue = charSet.contains('a');
// 挤压集合中任何一个重复字符
System.out.println("去重: " + CharSetUtils.squeeze("hello", "k-z"));// result:
// helo
System.out.println("计算字符串中包含某字符数: "
+ CharSetUtils.count("The lazy dog.", "aoei")); // result:3
System.out.println("删除字符串中某字符: "
+ CharSetUtils.delete("the lazy dog.", "aoei"));// result: th lzy dg
System.out.println("保留字符串中某字符: "
+ CharSetUtils.keep("the lazy dog.", "aoei"));// result: eao
System.out.println("合并重复的字符: "
+ CharSetUtils.squeeze("a babby c dd", "b d")); // result: a baby c d
System.out.println(CharSetUtils.translate("the lazy dog", "th", "ar"));// result: are lazy dog.
}
/**
* ObjectUtils
*
*/
private static void testObjectUtils() {
System.out.println(StringUtils.center("ObjectUtils", 40, "="));
Object obj = null;
System.out.println("Object为null时,默认打印某字符: "
+ ObjectUtils.defaultIfNull(obj, "空")); // result:空
System.out.println("验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.");
Object a = new Object();
Object b = a;
Object c = new Object();
System.out.println("a==b? " + ObjectUtils.equals(a, b)); // result: true
System.out.println("a==c? " + ObjectUtils.equals(a, c)); // result:
// false
System.out.println("用父类Object的toString()方法返回对象信息:"
+ ObjectUtils.identityToString(new Date())); // result:java.util.Date@1fae3c6
System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串."
+ ObjectUtils.toString(new Date())); // result: Tue Aug 04
// 16:22:53 CST 2015
System.out.println("为null时: " + ObjectUtils.toString(null)); // result:
ObjectUtils.toString(null, "nullStr");//
// ObjectUtils.max(Comparable c1, Comparable c2);
// ObjectUtils.min(Comparable c1, Comparable c2);
}
/**
* SerializationUtils
*/
private static void testSerializationUtils() {
System.out.println(StringUtils.center("SerializationUtils", 40, "="));
Date date = new Date();
byte[] bytes = SerializationUtils.serialize(date);
System.out.println("SerializationUtils.serialize: " + ArrayUtils.toString(bytes));
Date reDate = (Date) SerializationUtils.deserialize(bytes);
System.out.println("deserialize equals: " + ObjectUtils.equals(date, reDate)); // result: true
System.out.println("deserialize == :" + (date == reDate)); // result:false
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream(new File("d:/test.txt"));
fis = new FileInputStream(new File("d:/test.txt"));
SerializationUtils.serialize(date, fos);
Date reDate2 = (Date) SerializationUtils.deserialize(fis);
System.out.println("deserialize stream equals: " + date.equals(reDate2)); // result: true
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* RandomStringUtils
*/
private static void testRandomStringUtils(){
System.out.println(StringUtils.center("RandomStringUtils", 40, "="));
System.out.println("生成指定长度的随机字符串,好像没什么用: " + RandomStringUtils.random(10));
System.out.println("在指定字符串中生成长度为n的随机字符串:" + RandomStringUtils.random(5, "abcdefghijk"));
System.out.println("指定从字符或数字中生成随机字符串:" + RandomStringUtils.random(5, true, false)); //从字符
System.out.println("指定从字符或数字中生成随机字符串:" + RandomStringUtils.random(5, false, true)); //从数字
System.out.println("指定从字符或数字中生成随机字符串:" + RandomStringUtils.random(5,1,7, true, true,new char[]{'a','b','c','d','1','2','3'}));//one result:323bd
}
/**
* StringUtils
*/
private static void testStringUtils(){
System.out.println(StringUtils.center("StringUtils", 40, "="));
System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
String[] header = new String[3];
header[0] = StringUtils.repeat("**=**", 8);
header[1] = StringUtils.center(" StringUtils ", 40, "^O^");
header[2] = header[0];
System.out.println(StringUtils.join(header, "\n"));
System.out.println("缩短到某长度,用...结尾: " + StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 10)); // result: The qui...
System.out.println("缩短到某长度,用...结尾: " + StringUtils.abbreviate("The quick brown fox jumps over the lazy dog.", 3, 10)); //result: ... qui...
System.out.println("返回两字符串不同处索引号: " + StringUtils.indexOfDifference("aaabc", "aaacc")); //result: 3
System.out.println("返回两字符串不同处开始至结束: " + StringUtils.difference("aaabcde", "aaaccde")); // result: ccde
System.out.println("截去字符串为以指定字符串结尾的部分: " + StringUtils.chomp("aaabcde", "de")); //result: aaabc; aaabcdefg => aaabcdefg
System.out.println("检查一字符串是否为另一字符串的子集: " + StringUtils.containsOnly("aad", "aadd"));
System.out.println("检查一字符串是否不是另一字符串的子集: " + StringUtils.containsNone("defg", "aadd")); //result: false
System.out.println();
System.out.println("检查一字符串是否包含另一字符串: " + StringUtils.contains("defg", "ef")); //result: true
System.out.println("检查一字符串是否包含另一字符串: " + StringUtils.containsOnly("ef", "defg")); //result: true
System.out.println("返回可以处理null的toString(): " + StringUtils.defaultString("aaaa"));
System.out.println("返回可以处理null的toString(): " + "?" + StringUtils.defaultString(null) + "!");
System.out.println("去除字符中的空格:" + StringUtils.deleteWhitespace("aa bb cc")); //result: aabbcc
String[] strArray = StringUtils.split("a,b,,c,d,null, ", ",");
System.out.println("分隔符处理成数组:" + strArray.length); //result: 6
System.out.println("判断是否是某类字符. 字符: " + StringUtils.isAlpha("ab")); //result: true
System.out.println("判断是否是某类字符. 字符数字:" + StringUtils.isAlphanumeric("ab12")); //result: true
System.out.println("判断是否是某类字符.是不是空:" + StringUtils.isBlank("")); //result: true
System.out.println("判断是否是某类字符.是不是数字:" + StringUtils.isNumeric("123")); //result: true
}
/**
* SystemUtils
*/
private static void testSystemUtils() {
System.out.println(StringUtils.center("SystemUtils", 40, "="));
System.out.println("获得系统文件分隔符: " + SystemUtils.FILE_SEPARATOR);
System.out.println("获得源文件编码: " + SystemUtils.FILE_ENCODING);
System.out.println("获得ext目录: " + SystemUtils.JAVA_EXT_DIRS);
System.out.println("获得java版本: " + SystemUtils.JAVA_VM_VERSION);
System.out.println("获得java厂商: " + SystemUtils.JAVA_VENDOR);
}
/**
* testClassUtils
*/
private static void testClassUtils() {
System.out.println(StringUtils.center("ClassUtils", 40, "="));
System.out.println("获取类实现的所有接口: " + ClassUtils.getAllInterfaces(Date.class)); // [interface java.io.Serializable, interface java.lang.Cloneable, interface java.lang.Comparable]
System.out.println("获取类所有父类: " + ClassUtils.getAllSuperclasses(Date.class)); // [class java.lang.Object]
System.out.println("获取简单类名." + ClassUtils.getShortClassName(Date.class));// Date
System.out.println("获取包名: " + ClassUtils.getPackageName(Date.class)); //java.util
System.out.println("判断是否可以转型." + ClassUtils.isAssignable(Date.class, Object.class));
System.out.println("判断是否可以转型." + ClassUtils.isAssignable(Object.class, Date.class));
}
/**
* StringEscapeUtils
*/
private static void testStringEscapeUtils() {
System.out.println(StringUtils.center("StringEscapeUtils", 40, "="));
System.out.println("转换特殊字符 . html: "
+ StringEscapeUtils.escapeHtml("中国")); // result: 中国
System.out.println("转换特殊字符 . java: "
+ StringEscapeUtils.escapeJava("中国")); // result: \u4E2D\u56FD
System.out
.println("转换特殊字符 . sql: " + StringEscapeUtils.escapeSql("中国"));// result: 中国
System.out.println("转换特殊字符 . javascript: "
+ StringEscapeUtils.escapeJavaScript("中国")); // result: \u4E2D\u56FD
System.out
.println("转换特殊字符 . xml: " + StringEscapeUtils.escapeXml("中国")); // result: 中国
System.out.println("转换特殊字符 . html: "
+ StringEscapeUtils.unescapeHtml("中国")); // result: 中国
System.out.println("转换特殊字符 . java: "
+ StringEscapeUtils.unescapeJava("\u4E2D\u56FD")); // result: 中国
System.out.println("转换特殊字符 . javascript: "
+ StringEscapeUtils.unescapeJavaScript("\u4E2D\u56FD")); // result: 中国
System.out.println("转换特殊字符 . xml: "
+ StringEscapeUtils.unescapeXml("中国")); // result: 中国
}
/**
* NumberUtils
*/
private static void testNumberUtils() {
System.out.println(StringUtils.center("testNumberUtils", 40, "="));
System.out.println("字符串转为数字: " + NumberUtils.toInt("ba", 33));
System.out.println("从数组中选出最大值: "
+ NumberUtils.max(new int[] { 1, 2, 3, 4 }));
System.out.println("判断字符串是否全是整数: " + NumberUtils.isDigits("123.1"));
System.out.println("判断字符串是否是有效数字: " + NumberUtils.isNumber("0123.1"));
System.out.println("转换为数字: " + NumberUtils.createNumber("0x12"));
}
/**
* DateFormatUtils
*/
private static void testDateFormatUtils() {
System.out.println(StringUtils.center("SystemUtils", 40, "="));
System.out.println("格式化日期输出:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
System.out.println("格式化日期输出:" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
//format(long millis, String pattern, TimeZone timeZone)
//formatUTC(Date date/long millis/Calendar calendar, String pattern)
//format(Calendar calendar, String pattern, TimeZone timeZone, Locale locale)
System.out.println("秒表.");
StopWatch sw = new StopWatch();
sw.start();
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"));
}
sw.stop();
System.out.println("秒表计时:" + sw.getTime());
System.out.println("是不是同一天: " + DateUtils.isSameDay(new Date(), new Date()));//isSameDay(Calendar cal1, Calendar cal2)
//DateUtils。isSameInstant(cal1, cal2) 用的判断方式是cal1.getTime().getTime() == cal2.getTime().getTime();
//isSameLocalTime
// System.out.println(DateUtils.parseDate("2015-08-04", new String[]{"yyyy-MM-dd"}));
//DateUtils.addYears/addMonths/addWeeks/addDays/addHours/addMinutes/addSeconds/addMilliseconds
//DateUtils.setYears/setMonths/setDays/setHours/setMinutes/setSeconds/setMilliseconds/
// DateUtils.round(date/Calendar/Object, field)
// DateUtils.truncate(date/Calendar/Object, field)
System.out.println("FastDateFormat: " + FastDateFormat.getInstance("yyyy-MM-dd").format(new Date()) );
// DurationFormatUtils.formatDuration(durationMillis, format)
// DurationFormatUtils.formatPeriod(startMillis, endMillis, format)
System.out.println();
}
private static void testValidate() {
System.out.println(StringUtils.center("Validate", 40, "="));
Object[] strarray = { "a", "b", "c" };
Validate.notEmpty(strarray);// 返回void
// Validate.isTrue(expression)
// Validate.allElementsOfType(collection, clazz)
}
private static void testWordUtils() {
System.out.println(StringUtils.center("WordUtils", 40, "="));
String str1 = "wOrD";
String str2 = "ghj\nui\tpo";
System.out.println("首字母大写 : " + WordUtils.capitalize(str1)); // 首字母大写
System.out.println("首字母大写其它字母小写 : " + WordUtils.capitalizeFully(str1)); // 首字母大写其它字母小写
char[] ctrg = { '.' };
System.out.println("在规则地方转换: " + WordUtils.capitalizeFully("i aM.fine", ctrg)); // 在规则地方转换
System.out.println("获取首字母 : " + WordUtils.initials(str1)); // 获取首字母
System.out.println("取每个单词的首字母 : " + WordUtils.initials("Ben John Lee", null)); // 取每个单词的首字母
char[] ctr = { ' ', '.' };
System.out.println(" 按指定规则获取首字母 " + WordUtils.initials("Ben J.Lee", ctr)); // 按指定规则获取首字母
System.out.println(" 大小写逆转 : " + WordUtils.swapCase(str1)); // 大小写逆转
System.out.println("解析\\n和\\t等字符 : " + WordUtils.wrap(str2, 1)); // 解析\n和\t等字符
}