Hutool工具类使用01

  • Post author:
  • Post category:其他


  <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>
package com.dxy.study_0628.study.hut;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.FIFOCache;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.word.Word07Writer;
import cn.hutool.script.ScriptUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.system.SystemUtil;
import com.dxy.study_0628.domain.User;
import org.apache.poi.ss.extractor.ExcelExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.w3c.dom.Document;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class DateHul {
    public static void main(String[] args) {
        //字符串到时间
        String dataStr = "2020-09-23";
        DateTime parse = DateUtil.parse(dataStr);
        System.out.println(parse);
        //获取星座
        String zodiac = DateUtil.getZodiac(11, 10);
        System.out.println(zodiac);
        //获取生肖
        String chineseZodiac = DateUtil.getChineseZodiac(2021);
        System.out.println(chineseZodiac);
        //打印
        int ints[] = {1, 2, 3};
        Console.log(ints);
        FIFOCache<Object, Object> objects = CacheUtil.newFIFOCache(5);
        objects.put("1", "1");
        objects.put("2", "2");
        objects.put("3", new User(3, "tom", "123"));
        objects.put("4", "4");
        objects.put("5", "5");
        objects.put("6", "6");
        objects.put(3, "hello");
        Object o = objects.get("3");
        System.out.println(o);

        //编码转字符
        String hex = "e68891e698afe4b880e4b8aae5b08fe5b08fe79a84e58fafe788b1e79a84e5ad97e7aca6e4b8b2";
        //注意:在4.1.11之后hexStrToStr将改名为hexToStr
        String raw = Convert.hexToStr(hex, CharsetUtil.CHARSET_UTF_8);
        System.out.println(raw);

        //字符串转编码再转字符
        String a = "我是一个小小的可爱的字符串";
        //结果为:"\\u6211\\u662f\\u4e00\\u4e2a\\u5c0f\\u5c0f\\u7684\\u53ef\\u7231\\u7684\\u5b57\\u7b26\\u4e32"
        String unicode = Convert.strToUnicode(a);
        System.out.println(unicode);
        //结果为:"我是一个小小的可爱的字符串"
        String raw1 = Convert.unicodeToStr(unicode);
        System.out.println(raw1);

        //时间转换
        long a2 = 45353450;
        //结果为:75
        long minutes = Convert.convertTime(a2, TimeUnit.MILLISECONDS, TimeUnit.HOURS);
        System.out.println(minutes);

        double a3 = 67556.32;
        //结果为:"陆万柒仟伍佰伍拾陆元叁角贰分"
        String digitUppercase = Convert.digitToChinese(a3);
        Console.log(digitUppercase);

        String dateStr = "2020-01-23 12:23:56";
        DateTime dt = DateUtil.parse(dateStr);
        // Date对象转换为LocalDateTime
        LocalDateTime of = LocalDateTimeUtil.of(dt);
        System.out.println(of);
        // 时间戳转换为LocalDateTime
        of = LocalDateTimeUtil.ofUTC(dt.getTime());
        System.out.println(of);

        String template = "{}爱{},就像老鼠爱大米";
        String str = StrUtil.format(template, "我", "你"); //str -> 我爱你,就像老鼠爱大米
        Console.log(str);

        Digester md5 = new Digester(DigestAlgorithm.MD5);
        // 5393554e94bf0eb6436f240a4fd71282
        String digestHex = md5.digestHex("YUY");
        Console.log(digestHex);
        //读取excel文件
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file("F:\\xxxx\\test\\user\\excel.xls"),1);
        //ExcelReader reader = ExcelUtil.getReader(FileUtil.file("F:\\xxxx\\test\\user\\excel2.xlsx"));
        //ExcelReader reader = ExcelUtil.getReader(FileUtil.file("F:\\xxxx\\test\\user\\excel2.xlsx"), 0);
        System.out.println( reader.getExtractor().getText());
        ExcelExtractor extractor = reader.getExtractor();
        Sheet sheet = reader.getWorkbook().getSheetAt(0);
        String sheetName = sheet.getSheetName();
        int lastRowNum = sheet.getLastRowNum();
        System.out.println(sheetName+":"+lastRowNum);
        for (int i=0;i<lastRowNum;i++){
            Row row = sheet.getRow(i);
            Cell cell = row.getCell(0);
            Cell cell1 = row.getCell(1);
            Cell cell2 = row.getCell(2);
            Cell cell3 = row.getCell(3);
            System.out.println(cell+","+cell1+","+cell2+","+cell3);
        }
        //验证邮箱
        boolean isEmail = Validator.isEmail("loolly@gmail.com");
        System.out.println(isEmail);
        //获取properties中的数据
        Props prop = Props.getProp("xml/myconfig.properties","UTF-8");
        Object o1 = prop.get("flag");
        System.out.println(o1);
       
       
    }
}



https://blog.csdn.net/qq_42981242/article/details/110939210


https://zhengkai.blog.csdn.net/article/details/80087954?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link


工具类


https://blog.csdn.net/chinasi2012/article/details/108405457?spm=1001.2101.3001.6650.18&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-18.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-18.no_search_link



版权声明:本文为qq_40711092原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。