【java】org.apache.commons.lang3功能示例
org.apache.commons.lang3功能示例
package com.simple.test; import java.util.Date;
import java.util.Iterator;
import java.util.Map; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test; public class TestString { public static void main(String[] args) {
String[] test = { "33", "ddffd" };
String[] test2 = { "33", "ddffd" };
String[] test1 = { "ddffd", "33" }; // 1.判断两个数据是否相等, 如果内容相同, 顺序相同 则返回 真
System.out.println("判断两个数组是否相同: " + ArrayUtils.isEquals(test, test2));
System.out.println("判断数组中是否包含一个对象: " + ArrayUtils.contains(test, "33")); // 2.{33,ddffd} 将数组内容以{,}形式输出.
System.out.println("输出数组中的数据: "+ArrayUtils.toString(test)); System.out.println("讲一个二维数组转换成MAP....");
Map map = ArrayUtils.toMap(new String[][] { { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } });
// 3.toMap 一个数组,但每个元素 Each element of the array
// must be either a {@link java.util.Map.Entry} or an Array,
// 方式一 下面是遍历map的方式,取得其keySet.iterator();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
// it.next()只包含key
System.out.println("key:" + key + "value:" + map.get(key));
}
System.out.println("讲一个二维数组转换成MAP 打印结束....");
// 方式二,取得其entrySet()集合,
Iterator it1 = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it1.next();
// it1.next()中包含key和value
System.out.println("key :" + entry.getKey() + "value :" + entry.getValue());
} // 4.取得类名
System.out.println("取得一个类的名称: "+ ClassUtils.getShortClassName(Test.class));
// 取得其包名
System.out.println("取得一个类的包名: "+ ClassUtils.getPackageName(Test.class));
// 5.NumberUtils
System.out.println("将一个字符串转换成数字: "+ NumberUtils.toInt("6"));
System.out.println("将一个字符串转换成数字, 输入一个默认参数: "+ NumberUtils.toInt("7", 10));// 返回7 如果第一个参数为 null 返回10 // 6.五位的随机字母和数字
System.out.println("取得随机字母和数字: "+RandomStringUtils.randomAlphanumeric(15));
// 7.StringEscapeUtils
System.out.println(StringEscapeUtils.unescapeHtml3("</html>"));
// 输出结果为<html>
System.out.println(StringEscapeUtils.escapeJava("String"));
// 8.StringUtils,判断是否是空格字符
System.out.println("判断一个字符串是否是 空格: "+StringUtils.isBlank(" "));
// 将数组中的内容以,分隔
System.out.println("将数组中的内容以,分隔: "+ StringUtils.join(test, ","));
// 在右边加下字符,使之总长度为6
System.out.println("在右边加下字符,使之总长度为6: "+ StringUtils.rightPad("abc", 6, 'T'));
// 首字母大写
System.out.println("首字母大写: "+ StringUtils.capitalize("abc"));
// Deletes all whitespaces from a String 删除所有空格
System.out.println("删除所有空格 : "+ StringUtils.deleteWhitespace(" ab c "));
// 判断是否包含这个字符
System.out.println("判断是否包含这个字符 : "+ StringUtils.contains("abc", "ab"));
// 表示左边两个字符
System.out.println("取得一个字符串左边的两个字符: "+ StringUtils.left("abc", 2));
System.out.println("取得一个字符串右边的三个字符 : "+ StringUtils.right("abcd", 3)); System.out.println("把一个字符串转换为BigDecimal对象: " + NumberUtils.createBigDecimal("0.25"));
System.out.println("找出最大值: " + NumberUtils.max(new int[]{1,2,3}));
System.out.println("JavaHome: " + SystemUtils.getJavaHome());
System.out.println("临时目录位置: " + SystemUtils.getJavaIoTmpDir()); System.out.println("日期格式处理: " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
System.out.println("日期加 7天: " + DateFormatUtils.format(DateUtils.addDays(new Date(), 7), "yyyy-MM-dd HH:mm:ss")); }
}
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 org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.CharSet;
import org.apache.commons.lang3.CharSetUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.commons.lang3.StringEscapeUtils; /**
* @author Administrator
*http://commons.apache.org/proper/commons-lang/userguide.html
*/
public class LangDemo
{
public void charSetDemo()
{
System.out.println("**CharSetDemo**");
CharSet charSet = CharSet.getInstance("aeiou");
String demoStr = "The quick brown fox jumps over the lazy dog.";
int count = 0;
for (int i = 0, len = demoStr.length(); i < len; i++)
{
if (charSet.contains(demoStr.charAt(i)))
{
count++;
}
}
System.out.println("count: " + count);
} public void charSetUtilsDemo()
{
System.out.println("**CharSetUtilsDemo**");
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"));
} public void objectUtilsDemo()
{
System.out.println("**ObjectUtilsDemo**");
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));
System.out.println(ObjectUtils.equals(a, c)); System.out.println("用父类Object的toString()方法返回对象信息.");
Date date = new Date();
System.out.println(ObjectUtils.identityToString(date));
System.out.println(date); System.out.println("返回类本身的toString()方法结果,对象为null时,返回0长度字符串.");
System.out.println(ObjectUtils.toString(date));
System.out.println(ObjectUtils.toString(null));
System.out.println(date);
} public void serializationUtilsDemo()
{
System.out.println("*SerializationUtils**");
Date date = new Date();
byte[] bytes = SerializationUtils.serialize(date);
System.out.println(ArrayUtils.toString(bytes));
System.out.println(date); Date reDate = (Date) SerializationUtils.deserialize(bytes);
System.out.println(reDate);
System.out.println(ObjectUtils.equals(date, reDate));
System.out.println(date == reDate); 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(date.equals(reDate2)); }
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
fos.close();
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} } public void randomStringUtilsDemo()
{
System.out.println("**RandomStringUtilsDemo**");
System.out.println("生成指定长度的随机字符串,好像没什么用.");
System.out.println(RandomStringUtils.random(500)); System.out.println("在指定字符串中生成长度为n的随机字符串.");
System.out.println(RandomStringUtils.random(5, "abcdefghijk")); System.out.println("指定从字符或数字中生成随机字符串.");
System.out.println(RandomStringUtils.random(5, true, false));
System.out.println(RandomStringUtils.random(5, false, true)); } public void stringUtilsDemo()
{
System.out.println("**StringUtilsDemo**");
System.out.println("将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.");
String[] header = new String[3];
header[0] = StringUtils.repeat("*", 50);
header[1] = StringUtils.center(" StringUtilsDemo ", 50, "^O^");
header[2] = header[0];
String head = StringUtils.join(header, "/n");
System.out.println(head); System.out.println("缩短到某长度,用...结尾.");
System.out.println(StringUtils.abbreviate(
"The quick brown fox jumps over the lazy dog.", 10));
System.out.println(StringUtils.abbreviate(
"The quick brown fox jumps over the lazy dog.", 15, 10)); System.out.println("返回两字符串不同处索引号.");
System.out.println(StringUtils.indexOfDifference("aaabc", "aaacc")); System.out.println("返回两字符串不同处开始至结束.");
System.out.println(StringUtils.difference("aaabcde", "aaaccde")); System.out.println("截去字符串为以指定字符串结尾的部分.");
System.out.println(StringUtils.chomp("aaabcde", "de")); System.out.println("检查一字符串是否为另一字符串的子集.");
System.out.println(StringUtils.containsOnly("aad", "aadd")); System.out.println("检查一字符串是否不是另一字符串的子集.");
System.out.println(StringUtils.containsNone("defg", "aadd")); System.out.println("检查一字符串是否包含另一字符串.");
System.out.println(StringUtils.contains("defg", "ef"));
System.out.println(StringUtils.containsOnly("ef", "defg")); System.out.println("返回可以处理null的toString().");
System.out.println(StringUtils.defaultString("aaaa"));
System.out.println("?" + StringUtils.defaultString(null) + "!"); System.out.println("去除字符中的空格.");
System.out.println(StringUtils.deleteWhitespace("aa bb cc")); System.out.println("判断是否是某类字符.");
System.out.println(StringUtils.isAlpha("ab"));
System.out.println(StringUtils.isAlphanumeric("12"));
System.out.println(StringUtils.isBlank(""));
System.out.println(StringUtils.isNumeric("123"));
} public void systemUtilsDemo()
{
System.out.println(genHeader("SystemUtilsDemo"));
System.out.println("获得系统文件分隔符.");
System.out.println(SystemUtils.FILE_SEPARATOR); System.out.println("获得源文件编码.");
System.out.println(SystemUtils.FILE_ENCODING); System.out.println("获得ext目录.");
System.out.println(SystemUtils.JAVA_EXT_DIRS); System.out.println("获得java版本.");
System.out.println(SystemUtils.JAVA_VM_VERSION); System.out.println("获得java厂商.");
System.out.println(SystemUtils.JAVA_VENDOR);
} public void classUtilsDemo()
{
System.out.println(genHeader("ClassUtilsDemo"));
System.out.println("获取类实现的所有接口.");
System.out.println(ClassUtils.getAllInterfaces(Date.class)); System.out.println("获取类所有父类.");
System.out.println(ClassUtils.getAllSuperclasses(Date.class)); System.out.println("获取简单类名.");
System.out.println(ClassUtils.getShortClassName(Date.class)); System.out.println("获取包名.");
System.out.println(ClassUtils.getPackageName(Date.class)); System.out.println("判断是否可以转型.");
System.out.println(ClassUtils.isAssignable(Date.class, Object.class));
System.out.println(ClassUtils.isAssignable(Object.class, Date.class));
} public void stringEscapeUtilsDemo()
{
System.out.println(genHeader("StringEcsapeUtils"));
System.out.println("转换特殊字符.");
System.out.println("html:" + StringEscapeUtils.escapeHtml4("/n\n"));
System.out.println("html:"
+ StringEscapeUtils.unescapeHtml4("<p>"));
} private final class BuildDemo
{
String name; int age; public BuildDemo(String name, int age)
{
this.name = name;
this.age = age;
} public String toString()
{
ToStringBuilder tsb = new ToStringBuilder(this,
ToStringStyle.MULTI_LINE_STYLE);
tsb.append("Name", name);
tsb.append("Age", age);
return tsb.toString();
} public int hashCode()
{
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(name);
hcb.append(age);
return hcb.hashCode();
} public boolean equals(Object obj)
{
if (!(obj instanceof BuildDemo))
{
return false;
}
BuildDemo bd = (BuildDemo) obj;
EqualsBuilder eb = new EqualsBuilder();
eb.append(name, bd.name);
eb.append(age, bd.age);
return eb.isEquals();
}
} public void builderDemo()
{
System.out.println(genHeader("BuilderDemo"));
BuildDemo obj1 = new BuildDemo("a", 1);
BuildDemo obj2 = new BuildDemo("b", 2);
BuildDemo obj3 = new BuildDemo("a", 1); System.out.println("toString()");
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3); System.out.println("hashCode()");
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
System.out.println(obj3.hashCode()); System.out.println("equals()");
System.out.println(obj1.equals(obj2));
System.out.println(obj1.equals(obj3));
} public void numberUtils()
{
System.out.println(genHeader("NumberUtils"));
System.out.println("字符串转为数字(不知道有什么用).");
System.out.println(NumberUtils.toInt("ba", 33)); System.out.println("从数组中选出最大值.");
System.out.println(NumberUtils.max(new int[] { 1, 2, 3, 4 })); System.out.println("判断字符串是否全是整数.");
System.out.println(NumberUtils.isDigits("123.1")); System.out.println("判断字符串是否是有效数字.");
System.out.println(NumberUtils.isNumber("0123.1"));
} public void dateFormatUtilsDemo()
{
System.out.println(genHeader("DateFormatUtilsDemo"));
System.out.println("格式化日期输出.");
System.out.println(DateFormatUtils.format(System.currentTimeMillis(),
"yyyy-MM-dd HH:mm:ss")); 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()); } private String genHeader(String head)
{
String[] header = new String[3];
header[0] = StringUtils.repeat("*", 50);
header[1] = StringUtils.center(" " + head + " ", 50, "^O^");
header[2] = header[0];
return StringUtils.join(header, "/n");
} /**
* @param args
*/
public static void main(String[] args)
{
LangDemo langDemo = new LangDemo(); langDemo.charSetDemo();
langDemo.charSetUtilsDemo();
langDemo.objectUtilsDemo();
langDemo.serializationUtilsDemo();
langDemo.randomStringUtilsDemo();
langDemo.stringUtilsDemo();
langDemo.systemUtilsDemo();
langDemo.classUtilsDemo();
langDemo.stringEscapeUtilsDemo();
langDemo.builderDemo();
langDemo.numberUtils();
langDemo.dateFormatUtilsDemo(); } }
文转
【java】org.apache.commons.lang3功能示例的更多相关文章
- spring异常记录-----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
今天在练习怎样SSH中进行单元測试的时候出现下列异常: SEVERE: Exception starting filter Struts2 java.lang.NoClassDefFoundError ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
1.错误叙述性说明 2014-7-10 23:12:23 org.apache.catalina.core.StandardContext filterStart 严重: Exception star ...
- Hadoop java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
.jar 学习好友推荐案例的时候,提交运行时报错找不到StringUtils java.lang.ClassNotFoundException: org.apache.commons.lang3.St ...
- struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
2013-4-7 10:13:56 org.apache.catalina.startup.HostConfig checkResources 信息: Reloading context [/chap ...
- org.apache.commons.lang3.ArrayUtils 学习笔记
package com.nihaorz.model; /** * @作者 王睿 * @时间 2016-5-17 上午10:05:17 * */ public class Person { privat ...
- org.apache.commons.lang3.tuple.Pair 作为更新参数,XML 中的 Sql 取不到值、报错
项目用的 Mybatis,今天改一个需求,落地实现是批量更新,且只需要根据主键(id)来更新一个字段(name). 于是,没有犹豫,像下面这样设计了数据结构: 既然是批量更新,那外层肯定是 List ...
- NoClassDefFoundError: org/apache/commons/lang3/StringUtils
出错信息: 2014-2-5 21:38:05 org.apache.catalina.core.StandardContext filterStart严重: Exception starting f ...
- ERROR----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
2013-4-28 13:17:57 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filte ...
- org.apache.commons.lang3 的随机数生成
apache org.apache.commons.lang3 的随机数生成工具,方便使用. String a12 = RandomStringUtils.random(4, "012345 ...
随机推荐
- 排球积分规则功能说明书(spec)
排球规则: 由技术性规定.非技术性规定和场地设备要求等方面的内容组成的.每场比赛仍为五局三胜,前四局每局先得25分为胜,第五局先得15分者为胜.当出现24平或14平时,要继续比赛至领先2分才能取胜. ...
- [转]sed命令详解
转载:http://blog.chinaunix.net/u/22677/showart_1076318.html 1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保 ...
- python 字符串内建函数
方法 描述 string.capitalize() 把字符串的第一个字符大写 string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 string ...
- 总结之H3C汇聚层交换机认证在线人数展示系统
前情提要:意外接了老师说的一个小程序,然后计划7天(实际10天)的小项目就冒出来了. (1)时间与工程量.在和老师开始谈具体需求前,我凭感觉猜了猜完成这个小项目的时间.然后,再和老师确定需求后,再回头 ...
- 2.ViewBag、ViewData、TempData之间的区别
1.ViewBag and ViewData(非跨视图访问) 1)ViewBag是一种dynamic动态类型,用户可以自定义属性并为其赋值,它会在运行时动态解析(例:可以作为变量.数组等各种对象传递并 ...
- Yii框架(Yii Framework)部署
一.下载Yii 在部署yii框架之前首先要搭建好php环境,这里就不说搭建环境的问题了(这里已经部署好wampserver了),环境搭建好后,到yii官方网站下载yii framework:http: ...
- 关于thinkphp中的G方法使用
最近在研究thinkphp框架发现其中有好多东西很值得借鉴今天看了一下函数G()很适合调试出页面执行的时间和使用的内存情况具体的代码如下 <?php /** * 记录和统计时间(微秒)和内存使用 ...
- JAVA深入研究——Method的Invoke方法。
在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...
- logstash 添加nginx日志
选择需求分类废话少说直接上图 第一张图: 2.此图搭配的日志格式是: log_format main '$remote_addr - $remote_user [$time_local] $http_ ...
- eclipse控制台中文乱码解决方法
一.全局设置 1.Window > Preferences 2.General > Workspace > Text file encoding. 3.选择 Other 4.手工输入 ...