【Java tool class】HutoolUtil

maven import

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

Introduction of hutool-all

A Java basic tool class that encapsulates JDK methods such as file, stream, encryption and decryption, transcoding, regularization, thread, and XML to form various Util tool classes.

It is very powerful and encapsulates many components, so before implementing any tools, you might as well go to his official website to check: hutool-all official website

Contains components:

  • hutool-aop JDK dynamic proxy package, providing aspect support under non-IOC
  • hutool-bloomFilter Bloom filter, providing Bloom filter for some Hash algorithms
  • hutool-cache simple cache implementation
  • hutool-core core, including Bean operations, dates, various Utils, etc.
  • hutool-cron timing task module, providing timing tasks like Crontab expressions
  • hutool-crypto encryption and decryption module, providing symmetric, asymmetric and digest algorithm encapsulation
  • hutool-db JDBC encapsulated data operation, based on the idea of ​​ActiveRecord
  • hutool-dfa Multi-keyword search based on DFA model
  • hutool-extra extension module, for third-party packaging (template engine, email, Servlet, QR code, Emoji, FTP, word segmentation, etc.)
  • hutool-http Http client encapsulation based on HttpUrlConnection
  • hutool-log The log facade implemented by automatic log recognition
  • hutool-script script execution package, such as Javascript
  • hutool-setting More powerful Setting configuration file and Properties package
  • hutool-system System parameter call encapsulation (JVM information, etc.)
  • hutool-json JSON implementation
  • hutool-captcha image verification code implementation
  • hutool-poi package for Excel and Word in POI
  • hutool-socket Socket encapsulation of Java-based NIO and AIO
  • hutool-jwt JSON Web Token (JWT) encapsulation implementation

Common components

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author fei.chen
 * @description: HutoolUtil: Hutool中的工具类很多,可以参考官网:https://www.hutool.cn/
 * @date 2023/5/11下午 4:50
 */
public class HutoolUtil {
    
    

    static class HutDateUtil {
    
    

        public static void dateTest() {
    
    
            //Date、long、Calendar之间的相互转换
            //当前时间
            Date date = DateUtil.date();
            //Calendar转Date
            date = DateUtil.date(Calendar.getInstance());
            //时间戳转Date
            date = DateUtil.date(System.currentTimeMillis());
            //自动识别格式转换
            String dateStr = "2017-03-01 12:22:22";
            date = DateUtil.parse(dateStr);
            //自定义格式化转换
            date = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss");
            //格式化输出日期
            String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
            //获得年的部分
            int year = DateUtil.year(date);
            //获得月份,从0开始计数
            int month = DateUtil.month(date);
            //获取某天的开始、结束时间
            Date beginOfDay = DateUtil.beginOfDay(date);
            Date endOfDay = DateUtil.endOfDay(date);
            //计算偏移后的日期时间
            Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
            //计算日期时间之间的偏移量
            long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
        }
    }

    static class HutStrUtil {
    
    
        public static void strTest() {
    
    
            //判断是否为空字符串
            String str = "test";
            StrUtil.isEmpty(str);
            StrUtil.isNotEmpty(str);
            //去除字符串的前后缀
            StrUtil.removeSuffix("a.jpg", ".jpg");
            StrUtil.removePrefix("a.jpg", "a.");
            //格式化字符串
            String template = "这只是个占位符:{}";
            String str2 = StrUtil.format(template, "我是占位符");
            System.out.println(str2);
        }
    }

    static class HutNumberUtil{
    
    
        public static void numberTest() {
    
    
            double n1 = 1.234;
            double n2 = 1.234;
            double result;
            //对float、double、BigDecimal做加减乘除操作
            result = NumberUtil.add(n1, n2);
            result = NumberUtil.sub(n1, n2);
            result = NumberUtil.mul(n1, n2);
            result = NumberUtil.div(n1, n2);
            //保留两位小数
            BigDecimal roundNum = NumberUtil.round(n1, 2);
            String n3 = "1.234";
            //判断是否为数字、整数、浮点数
            NumberUtil.isNumber(n3);
            NumberUtil.isInteger(n3);
            NumberUtil.isDouble(n3);
            //BeanUtil
            // JavaBean的工具类,可用于Map与JavaBean对象的互相转换以及对象属性的拷贝。

            TestClass testClass = new TestClass();
            testClass.setId(1L);
            testClass.setName("大大");
            testClass.setShowStatus(0);
            //Bean转Map
            Map<String, Object> map = BeanUtil.beanToMap(testClass);
            String template = "beanUtil bean to map:{}";
            System.out.println(StrUtil.format(template, map));
            //Map转Bean
            TestClass mapTestClass = BeanUtil.mapToBean(map, TestClass.class, false);
            System.out.println(StrUtil.format("beanUtil map to bean:{}", mapTestClass));
            //Bean属性拷贝
            TestClass copyTestClass = new TestClass();
            BeanUtil.copyProperties(testClass, copyTestClass);
            System.out.println(StrUtil.format("beanUtil copy properties:{}", copyTestClass));
        }
    }

    static class HutMapUtil{
    
    
        public static void mapTest() {
    
    
            //将多个键值对加入到Map中
            Map<Object, Object> map = MapUtil.of(new String[][]{
    
    
                    {
    
    "key1", "value1"},
                    {
    
    "key2", "value2"},
                    {
    
    "key3", "value3"}
            });
            //判断Map是否为空
            MapUtil.isEmpty(map);
            MapUtil.isNotEmpty(map);
//            AnnotationUtil
//            注解工具类,可用于获取注解与注解中指定的值。

            //获取指定类、方法、字段、构造器上的注解列表
            Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class, false);
            System.out.println(StrUtil.format("annotationUtil annotations:{}", annotationList));
            //获取指定类型注解
            Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
            System.out.println(StrUtil.format("annotationUtil api value:{}", api.description()));
            //获取指定类型注解的值
            Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);
        }
    }

    /**
     * 加密解密工具类
     */
    static class HutSecureUtil{
    
    
        public static void md5Test() {
    
    
            //MD5加密
            String str = "123456";
            String md5Str = SecureUtil.md5(str);
        }

        public static void sh1Test() {
    
    
            //sha1加密
            String str = "123456";
            String sha1Str = SecureUtil.sha1(str);
        }
    }
}

Guess you like

Origin blog.csdn.net/daohangtaiqian/article/details/130626456