开发喵星球

工具类集合

image-20231116163327935

项目地址:https://www.hutool.cn/

依赖

Maven

在项目的pom.xml的dependencies中加入以下内容:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

判断字符串是否位为空

 String str1=null;
        System.out.println(StrUtil.isEmptyIfStr(str1));
        System.out.println(StrUtil.isNotEmpty(str1));

判断是否为数字

    String str2="abc";
    System.out.println(StrUtil.isNumeric(str2));
    String str3="123";
    System.out.println(StrUtil.isNumeric(str3));

去除空格和回车

    String str4=" 123\n";
    System.out.println(StrUtil.cleanBlank(str4));

字符串替换

    String str5="123456";
    System.out.println(StrUtil.replace("123456", "123", "abc"));

正则表达式ReUtil,判断是否是IP地址

 String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
System.out.println(ReUtil.isMatch(ip,"192.168.1.1"));

集合工具ListUtil

    List<String> list1= ListUtil.of("a","c","b");
    System.out.println(list1);      

集合排序

    List<String> list3=ListUtil.toList("a","b","c");
    List<String> list4=ListUtil.sortByProperty(list3, "order");
    System.out.println(list4);

集合去重

    List<String> list5=ListUtil.toList("a","b","c","c");
    List<String> list6= CollectionUtil.distinct(list3);
    System.out.println(list4);

日期格式化

    String strDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    System.out.println(strDate);  // 输出:2023-06-14 10:28:20

日期计算

    Date date = DateUtil.offsetDay(new Date(), -1);  // 昨天的日期
    System.out.println(date);  // 输出:Mon Jun 13 10:28:20 CST 2023

日期比较

    boolean isSameDay = DateUtil.isSameDay(new Date(), DateUtil.parse("2023-06-14"));
    System.out.println(isSameDay);  // 输出:true

MD5加密

    String md5Str = SecureUtil.md5("hutool");
    System.out.println(md5Str);  // 输出:a9d4bbde746b87e82f11c8dfb5cf1576

SHA1加密

    String sha1Str = SecureUtil.sha1("hutool");
    System.out.println(sha1Str);  // 输出:a5eeabe8bc7be3433fdec00a0f8dbf9b21b1b246

发送GET请求

    String result = HttpUtil.get("https://www.baidu.com");
    System.out.println(result);

发送POST请求

String postData = "username=admin&password=123456";
result = HttpUtil.post("http://localhost:8080/login", postData);

生成随机整数

    int randomInt = RandomUtil.randomInt(1, 100);
    System.out.println(randomInt);  // 输出:随机整数(1-100)

生成随机浮点数

    double randomDouble = RandomUtil.randomDouble(0, 1);
    System.out.println(randomDouble);  // 输出:随机浮点数(0-1)

生成指定长度的随机字符串

    String randomString = RandomUtil.randomString(10);
    System.out.println(randomString);  // 输出:随机字符串(长度为10)

判断对象是否为空或null

    boolean isNull = ObjectUtil.isNull(null);
    System.out.println(isNull);  // 输出:true
    isNull = ObjectUtil.isNull(new Object());
    System.out.println(isNull);  // 输出:false

比较两个对象是否相等

    boolean isEqual = ObjectUtil.equal("hutool", "Hutool");
    System.out.println(isEqual);  // 输出:false

字符串转换为整数

    int intValue = Convert.toInt("123");
    System.out.println(intValue);  // 输出:123

整数转换为字符串

    String strValue = Convert.toStr(123);
    System.out.println(strValue);  // 输出:"123"

字符串转换为日期

    String dateStr = "2023-06-14";
    Date date1 = Convert.toDate(dateStr);
    System.out.println(date1);  // 输出:Wed Jun 14 00:00:00 CST 2023

日期转换为字符串

    String dateStr2 = Convert.toStr(date, "yyyy-MM-dd");
    System.out.println(dateStr2);  // 输出:"2023-06-14"

验证邮箱格式是否正确

    boolean isEmail = Validator.isEmail("123@qq.com");
    System.out.println(isEmail);  // 输出:true

验证手机号码是否正确

    boolean isMobile = Validator.isMobile("13888888888");
    System.out.println(isMobile);  // 输出:true

验证身份证号码是否正确

    boolean isCitizenId = Validator.isCitizenId("110101199003077777");
    System.out.println(isCitizenId);  // 输出:true

    final Log log = LogFactory.get();  // 获取Logger对象

    log.debug("debug message");
    log.info("info message");
    log.warn("warn message");
    log.error("error message");

json工具

    String jsonStr = "{\"name\":\"kaifamiao\",\"age\":18}";
    JSONObject json = JSONUtil.parseObj(jsonStr);
    System.out.println(json);
    System.out.println(json.get("age"));
    System.out.println(json.get("name"));  // 输出:kaifamiao

生成JSON字符串

    JSONObject json2 = JSONUtil.createObj()
            .put("name", "kaifamiao")
            .put("age", 18);
    String jsonStr2 = json2.toString();
    System.out.println(jsonStr2);  // 输出:{"age":18,"name":"kaifamiao"}

    HttpResponse sp = HttpUtil.createPost("http://kaifamiao.dev").execute();
    String body = sp.body();
    System.out.println(body);

发送HTTP POST请求

    Map<String, Object> params = new HashMap<>();
    params.put("name", "kaifamiao");
    params.put("age", 18);
    HttpResponse response = HttpUtil.createPost("https://kaifamiao.dev/").form(params).execute();
    String body2 = response.body();
    System.out.println(body2);

下载网络文件

    HttpUtil.downloadFile("https://example.com/image.png", FileUtil.file("image.png"));
   
分类:技术栈 作者:开发喵 发表于:2023-11-16 17:54:08 阅读量:90
<<   >>


powered by kaifamiao