0


org.apache.commons.lang3工具类使用

org.apache.commons.lang3工具类使用

首先需要引入依赖

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version></dependency>

常用方法如下:

/**
     *org.apache.commons.lang3.StringUtils 方法
     */@TestvoidcontextLoads()throwsParseException,InterruptedException{// ************************ 字符串 ***********************************// 有一个为空则为trueSystem.out.println("isAnyBlank:"+StringUtils.isAnyBlank("","4","5"));// 全部不为空则为trueSystem.out.println("isNoneBlank:"+StringUtils.isNoneBlank("4","5"));// 全部为空则为trueSystem.out.println("isAllBlank:"+StringUtils.isAllBlank(""," "));// 如果字符串尚未以后缀结尾,则在字符串结尾追加后缀System.out.println("appendIfMissing:"+StringUtils.appendIfMissing("hello","@"));// hello@// 指定字符串不以某段字符串开头,则自动添加开头System.out.println("prependIfMissing :"+StringUtils.prependIfMissing("hello","@"));// @hello// 判断字符串是否以什么结尾System.out.println("endsWith:"+StringUtils.endsWith("abcd.s@","@"));/**
         *  截取字符串做一个缩减操作  固定占位符 ... 站三个宽度
         *  也就是最终会有三个点,最大宽度为总共几个包括...
         *  且最大宽度最小为4,如果最大长度大于等于 源字符串则不生效相当于没有进行操作
         */System.out.println("abbreviate:"+StringUtils.abbreviate("测试字 符串",6));// abbreviate:测...System.out.println("abbreviate:"+StringUtils.abbreviate("abcd.s@",5));// abbreviate:ab...// 首字母转大写System.out.println("capitalize:"+StringUtils.capitalize("abcd.s@"));// 首字母转小写System.out.println("uncapitalize:"+StringUtils.uncapitalize("abcd.s@"));// 字符串中间填充(长度包含源字符串长度)System.out.println("center :"+StringUtils.center("测试字 符串",8,"*"));// 字符串左填充System.out.println("leftPad :"+StringUtils.leftPad("测试字 符串",5,"*"));// 字符串右填充System.out.println("rightPad :"+StringUtils.rightPad("测试字 符串",6,"*"));// 从字符串中删除最后一个换行符\n、\r、\r\nSystem.out.println("chomp :"+StringUtils.chomp("abcd.@569 \r"));// 剔除字符串最后一个字符(如果最后一个是\n或\r\或\r\n也剔除掉)System.out.println("chop :"+StringUtils.chop("abcd.@569 \r"));// 删除字符串中的空白全部 java 的trim只去除首尾的System.out.println("deleteWhitespace :"+StringUtils.deleteWhitespace("测试字 符串"));// 比较两个字符串,并返回第二个字符串的剩余部分,从它与第一个不同的地方开始System.out.println("difference :"+StringUtils.difference("abcd.s@","abcd.@569 \r"));// 获得多个字符串相同的开头内容,接收参数为多个字符串System.out.println("getCommonPrefix :"+StringUtils.getCommonPrefix("abc","ac","abcd"));// a// 获取字符串中的数字System.out.println("getDigits :"+StringUtils.getDigits("ckds123456cs6.9dkcm"));// 12345669// 给字符串数组添加知道分隔符System.out.println("joinWith :"+StringUtils.joinWith(",","abc25","abc"));// abc25,abc// 字符串截取相当于substring,不同在于当截取字符超过源字符串长度不会报错System.out.println("mid :"+StringUtils.mid("asdfg",1,8));// sdfg// 用另一个字符串覆盖字符串的一部分(指定区域)System.out.println("overlay :"+StringUtils.overlay("abcdef","zzzz",2,4));// abzzzzef//替换字符串内容……等同于java 原生 replace()和 replaceFirst()System.out.println("replace :"+StringUtils.replace("aba","a","z"));// zbzSystem.out.println("replaceOnce :"+StringUtils.replaceOnce("aba","a","z"));// zba// //统计一字符串在另一字符串中出现次数System.out.println("countMatches :"+StringUtils.countMatches("dcabcfccrt","c"));// 4// 删除字符串中的指定内容System.out.println("remove :"+StringUtils.remove("abcdef","c"));// abdef// 删除字符串中结尾(满足是以某段内容结尾)System.out.println("removeEnd :"+StringUtils.removeEnd("abcdef","ef"));// abcd// 删除字符串中开头(满足是以某段内容开始)System.out.println("removeStart :"+StringUtils.removeStart("abcdef","a"));// bcdef// //重复字符/字符串System.out.println("repeat :"+StringUtils.repeat("abcdef",5));// abcdefabcdefabcdefabcdefabcdef// 字符串翻转System.out.println("reverse :"+StringUtils.reverse("abcdef"));// fedcba// 根据指定分隔符进行反转,分隔符之间的字符不进行反转System.out.println("reverseDelimited :"+StringUtils.reverseDelimited("abc,def",','));// def,abc// 将字符串后面指定个数的字符翻转到前面System.out.println("rotate :"+StringUtils.rotate("abc,defgfd",3));// gfdabc,def// 截取指定字符串前面的内容System.out.println("substringBefore :"+StringUtils.substringBefore("abcba","b"));// a// 截取最后指定字符串前面的内容System.out.println("substringBeforeLast :"+StringUtils.substringBeforeLast("abcba","b"));// abc// 截取指定字符串后面的内容System.out.println("substringAfter :"+StringUtils.substringAfter("abcba","b"));// cab// 截取最后指定字符串后面的内容System.out.println("substringAfterLast :"+StringUtils.substringAfterLast("abcba","b"));// a// 截取指定字符串中间的内容System.out.println("substringBetween :"+StringUtils.substringBetween("tagabctag","tag"));// abcSystem.out.println("substringBetween :"+StringUtils.substringBetween("yabczyabcz","y","z"));// abc// 截断字符串 最大宽度科超过源字符串长度不会报错System.out.println("truncate :"+StringUtils.truncate("abcba",6));// abcba// 判断字符串数字System.out.println("isNumeric :"+StringUtils.isNumeric("12 3"));// falseSystem.out.println("isNumericSpace :"+StringUtils.isNumericSpace("12 3"));// true// 判断字符串是否只有字母和数字组成 汉字可通过System.out.println("isAlphanumeric :"+StringUtils.isAlphanumeric("12xs好j3"));// true// 判断字符串是否只有字母和数字和空格组成 汉字可通过System.out.println("isAlphanumericSpace :"+StringUtils.isAlphanumericSpace("12xs 好j3"));// true// 字符串截取,不会忽略空数据System.out.println("splitByWholeSeparator :"+Arrays.asList(StringUtils.splitByWholeSeparator("12x,s 好j3,,",",")));// [12x, s 好j3, ]// ************************ 数字 ***********************************// //判断字符串是否全是整数System.out.println("isDigits :"+NumberUtils.isDigits("12.3"));// false//从数组中选出最大值System.out.println("max :"+NumberUtils.max(1,2,3,4));// 4//从数组中选出最小值System.out.println("min :"+NumberUtils.min(1,2,3,4));// 1//将一个字符串转换为int类型,失败返回0System.out.println("toInt :"+NumberUtils.toInt("123"));// 123//将一个字符串转换为int类型,失败返回自定义System.out.println("toDouble :"+NumberUtils.toDouble("123.1",0));// 123.1//将一个字符串转换为BigDecimal,自定义小数位数,自定义舍入模式,默认保留2位小数,舍入模式为RoundingMode.HALF_EVENSystem.out.println("toScaledBigDecimal :"+NumberUtils.toScaledBigDecimal("2.1",2,RoundingMode.HALF_UP));// 2.10// ,判断一个字符串是否可转化为数字isParsable / isCreatableSystem.out.println("isParsable :"+NumberUtils.isParsable("3.26"));// trueSystem.out.println("isCreatable :"+NumberUtils.isCreatable("3.26"));// true// ************************ 数组 ***********************************//创建数组String[] array =ArrayUtils.toArray("1","2");System.out.println("判断数组中是否包含某一对象 :"+ArrayUtils.contains(array,"33"));// ************************ 日期工具 ***********************************System.out.println("Date 转化为字符串 :"+DateFormatUtils.format(newDate(),"yyyy-MM-dd HH:mm:ss"));System.out.println("字符串 转 Date :"+DateUtils.parseDate("2022-12-19 22:00:00","yyyy-MM-dd HH:mm:ss"));Date now =newDate();Date addDays =DateUtils.addDays(now,1);System.out.println("Date 加 1 天 :"+ addDays);Date addMinutes =DateUtils.addMinutes(now,30);System.out.println("Date 加 30 分钟 :"+ addMinutes);Date addSeconds =DateUtils.addSeconds(now,-180);System.out.println("Date 减去 180 秒 :"+ addSeconds);System.out.println("// 判断是否 为 同一天 :"+DateUtils.isSameDay(addDays, addMinutes));// 过滤时分秒,若 now 为 2020-05-07 22:13:00 调用 truncate 方法以后// 返回时间为 2020-05-07 00:00:00Date truncate =DateUtils.truncate(now,Calendar.DATE);// ************************ 时间计算 ***********************************// 创建之后立刻计时StopWatch watch =StopWatch.createStarted();// 若想主动开始计时,创建计时器,调用 start 方法开始计时StopWatch watch1 =StopWatch.create();
        watch1.start();// 模拟其他代码耗时TimeUnit.SECONDS.sleep(2L);System.out.println("统计从开始到现在运行时间:"+ watch.getTime()+"ms");//2000msTimeUnit.SECONDS.sleep(2L);
        watch.split();System.out.println("从start到此刻为止的时间:"+ watch.getTime());//4000System.out.println("从开始到第一个切入点运行时间:"+ watch.getSplitTime());//4000Thread.sleep(1000);
        watch.split();System.out.println("从开始到第二个切入点运行时间:"+ watch.getSplitTime());//5001

        watch.reset();//重置后必须使用start方法
        watch.start();Thread.sleep(1000);System.out.println("重新开始后到当前运行时间是:"+ watch.getTime());//1000

        watch.suspend();//暂停Thread.sleep(6000);//模拟暂停6秒钟

        watch.resume();//上面suspend,这里要想重新统计,需要恢复一下System.out.println("恢复后执行的时间是:"+ watch.getTime());//1000  注意此时这个值还是1000

        watch.stop();System.out.println("花费的时间 ==>"+ watch.getTime()+"ms");//1000msSystem.out.println("花费的时间 ==>"+ watch.getTime(TimeUnit.SECONDS)+"s");//1s 可以直接转成s}
标签: apache java 前端

本文转载自: https://blog.csdn.net/rq12345688/article/details/128375650
版权归原作者 沙皮狗你不懂 所有, 如有侵权,请联系我们删除。

“org.apache.commons.lang3工具类使用”的评论:

还没有评论