Spring中常用的工具类StringUtils、DateUtils、CollectionUtils等

一、StringUtils

1、Spring提供的StringUtils

复制代码
 /**
     * Spring自带的StringUtils
     */
    @Test
    void testStringUtils01(){
        boolean b = StringUtils.hasText("  "); //可用
        boolean empty = StringUtils.isEmpty("  "); //可用
        System.out.println(b); //false  认为没有文本  我们想要的
        System.out.println(empty); //false  认为不为空   空格不是null 也是我们想要的

        //测试结果  不是想要的结果
        String[] split = StringUtils.split("ni shi ge cao bao", " ");
        assert split != null;
        for (String s:split) {
            System.out.println(s);
        }

        //测试结果 单词之间的空格是一个没有问题,多个空格就报错
        String[] strings = StringUtils.tokenizeToStringArray("ni shi cao bao", " ");
        for (String s : strings) {
            System.out.println(s);
        }
    }
复制代码

2、Commons-lang3 提供的StringUtils工具类

复制代码
   /**
     * commons-lang3 提供的StringUtils工具类
     */
    @Test
    void testStringUtils02() {
        boolean notBlank = StringUtils.isNotBlank("  ");
        boolean blank = StringUtils.isBlank("  ");
        System.out.println(notBlank);//false
        System.out.println(blank);//true
        boolean empty = StringUtils.isEmpty(" ");
        System.out.println(empty);//false
        boolean contains1 = StringUtils.contains("你是哪里人", "你");
        boolean contains2 = StringUtils.contains("你是哪里人", "哪里人");
        System.out.println(contains1);//true
        System.out.println(contains2);//true

        String replace1 = StringUtils.replace("你是哪里人 ,你是哪里人", "哪里人", "我的心上人");
        System.out.println(replace1);//你是我的心上人 ,你是我的心上人

        String replace2 = StringUtils.replaceOnce("你是哪里人 ,你是哪里人", "你是哪里人", "心上人");
        System.out.println(replace2);//心上人 ,你是哪里人

        int compare1 = StringUtils.compare("you are in my heart", "you are in my heart");
        int compare2 = StringUtils.compareIgnoreCase("you are in my heart", "you are iN my heart");

        System.out.println(compare1); //0
        System.out.println(compare2); //0

        boolean b = StringUtils.startsWith("you are in my heart", "you");
        System.out.println(b);//true
    }
复制代码

二、DateUtils

 

1、Commons-lang3 提供的DateUtils工具类

复制代码
   /**
     * DateUtils
     */
    @Test
    void testDate(){
        Date date = new Date();
        Date date1 = DateUtils.addDays(date, 1);
        Date date2 = DateUtils.addHours(date1, 10);
        boolean sameDay = DateUtils.isSameDay(date1, date2);
        Date date3 = DateUtils.setYears(date1, 2030);
        System.out.println(sameDay);
        System.out.println(date3);
    }
复制代码

三、CollectionUtils(以下都是Spring框架提供的)

1、比较集合中的元素

复制代码
 /**
     * 比较两个集合中的元素
     */
    @Test
    void contextLoads() {
        List<String> list1 = new ArrayList<String>();
        list1.add("1");
        list1.add("2");
        List<String> list2 = new ArrayList<>();
        list2.add("1");
        list2.add("3");
        list2.add("4");
        //并集
        Collection diff1 = CollectionUtils.union(list1, list2);
        System.out.println(diff1.toString()); //[1, 2, 3, 4]
        //交集
        Collection diff2 = CollectionUtils.intersection(list2, list1);
        System.out.println(diff2.toString()); //[1]
        //交集的补集
        Collection diff3 = CollectionUtils.disjunction(list1, list2);
        System.out.println(diff3); //[2, 3, 4]
        //list1与list2的差
        Collection diff4 = CollectionUtils.subtract(list1, list2);
        System.out.println(diff4); //[2]
        //list2与list1的差
        Collection diff5 = CollectionUtils.subtract(list2, list1);
        System.out.println(diff5); //[3, 4]
    }
复制代码

2、是否为空判断

复制代码
    /**
     * CollectionUtils
     */
    @Test
    void testCollections(){
        //判断HashMap是否为空
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        boolean empty = CollectionUtils.isEmpty(objectObjectHashMap);
        System.out.println(empty);//true

        //判断ArrayList是否为空
        ArrayList<Object> objects = new ArrayList<>();
        boolean empty1 = CollectionUtils.isEmpty(objects);
        System.out.println(empty1);//true

        //数组转集合
        String[] a ={"wo","he","ni"};
        List list = CollectionUtils.arrayToList(a);
        Object o2 = CollectionUtils.lastElement(list);
        System.out.println(list);//[wo, he, ni]
        System.out.println(o2);//ni
    }
复制代码

四、AlternativeJdkIdGenerator(UUID工具类生成)

复制代码
   /**
     * UUID工具类生成
     */
    @Test
    void testAlternativeJdkIdGenerator(){
        AlternativeJdkIdGenerator a=new AlternativeJdkIdGenerator();
        UUID uuid = a.generateId();
        String id = StringUtils.replace(uuid.toString(), "-", "");
        System.out.println(id);//生成去掉“-”的32位uuid  例如:b079c3d726b5b7ae6d432d2e2a0831b3
    }
复制代码

五、PathMatcher(路径匹配器)

复制代码
    /**
     * PathMatcher
     */
    @Test
    void testPathMatcher(){
        PathMatcher pathMatcher = new AntPathMatcher();
        System.out.println();

        // 精确匹配
        System.out.println(pathMatcher.match("/test", "/test"));//true
        System.out.println(pathMatcher.match("test", "/test"));//false

        //测试占位符?
        System.out.println(pathMatcher.match("t?st", "test"));//true
        System.out.println(pathMatcher.match("te??", "test"));//true
        System.out.println(pathMatcher.match("tes?", "tes"));//false
        System.out.println(pathMatcher.match("tes?", "testt"));//false

        //测试通配符*
        System.out.println(pathMatcher.match("*", "test"));//true
        System.out.println(pathMatcher.match("test*", "test"));//true
        System.out.println(pathMatcher.match("test/*", "test/Test"));//true
        System.out.println(pathMatcher.match("*.*", "test."));//true
        System.out.println(pathMatcher.match("*.*", "test.test.test"));//true
        System.out.println(pathMatcher.match("test*", "test/")); //注意这里是false 因为路径不能用*匹配
        System.out.println(pathMatcher.match("test*", "test/t")); //false这同理
        System.out.println(pathMatcher.match("test*aaa", "testblaaab")); //这个是false 因为最后一个b无法匹配了 前面都是能匹配成功的

        //测试通配符** 匹配多级URL
        System.out.println(pathMatcher.match("/*/**", "/testing/testing"));//true
        System.out.println(pathMatcher.match("/**/*", "/testing/testing"));//true
        System.out.println(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla")); //这里也是true哦
        System.out.println(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test"));//false

        System.out.println(pathMatcher.match("/????", "/bala/bla"));//false
        System.out.println(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb"));//false

        System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/"));//true
        System.out.println(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing"));//true
        System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing"));//true
        System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg"));//true
        System.out.println(pathMatcher.match("/foo/bar/**", "/foo/bar"));//true

        //这个需要特别注意:{}里面的相当于Spring MVC里接受一个参数一样,所以任何东西都会匹配的
        System.out.println(pathMatcher.match("/{bla}.*", "/testing.html"));//true
        System.out.println(pathMatcher.match("/{bla}.htm", "/testing.html")); //这样就是false了
    }
复制代码

 

posted @   donleo123  阅读(2071)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示