日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

hutool工具類 | huTool的基本使用

作者:m0_59259076 更新時間: 2023-10-11 編程語言
依賴包:
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.1.0</version>
</dependency>
package com.zhang.common;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.zhang.entity.Animal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

public class Test {

    private static final String[] STRING_ARRAY = {"1", "2", "3"};

    public static void main(String[] args) {
        // Convert.class
        System.out.println("=======Convert.class===========");
        // 1、數(shù)字(金額)轉(zhuǎn)換漢字
        double amount = 10500.56;
        String digitToChinese = Convert.digitToChinese(amount);
        System.out.println("1、數(shù)字(金額)轉(zhuǎn)換漢字:\n" + digitToChinese + "\n");  // 壹萬零伍佰元伍角陸分
        //   2、數(shù)字轉(zhuǎn)換為字符串
        int num = 2;
        String toStr = Convert.toStr(num);
        System.out.println("2、數(shù)字轉(zhuǎn)換為字符串:\n" + toStr + "\n"); // 2

        // 3、字符串?dāng)?shù)組轉(zhuǎn)換為指定類型的數(shù)組
        Integer[] intArray = Convert.toIntArray(STRING_ARRAY);
        System.out.println("3、字符串?dāng)?shù)組轉(zhuǎn)換為指定類型的數(shù)組:");
        for (Integer integer : intArray) {
            System.out.print(integer + ", "); // 1  2
        }
        System.out.println("\n");

        // 4、字符串轉(zhuǎn)日期對象
        Date date_ = Convert.toDate("2022-04-08");
        System.out.println("4、Convert.toDate(\"2022-04-08\"):\n" + date_ + "\n"); //

        // 5、字符串?dāng)?shù)組轉(zhuǎn)List列表
        List<String> stringList = Convert.toList(String.class, STRING_ARRAY);
        System.out.println("5、字符串?dāng)?shù)組轉(zhuǎn)List列表");
        stringList.forEach(System.out::println);

        //
        System.out.println("========DateUtil.class=========");
        // 6、獲取當(dāng)前時間
        System.out.println("6、獲取當(dāng)前時間:\n" + DateUtil.date() + "\n");

        Date currentDate;
        // 7、Calendar轉(zhuǎn)Date
        currentDate = DateUtil.date(Calendar.getInstance());
        System.out.println("7、Calendar轉(zhuǎn)Date:\n" + currentDate + "\n");

        // 8、時間戳轉(zhuǎn)Date
        currentDate = DateUtil.date(System.currentTimeMillis());
        System.out.println("8、時間戳轉(zhuǎn)Date:\n" + currentDate + "\n");

        // 9、自動識別格式轉(zhuǎn)換
        currentDate = DateUtil.parse("2022-04-08");
        System.out.println("9、自動識別格式轉(zhuǎn)換:\n" + currentDate + "\n");

        // 10、自定義格式化轉(zhuǎn)換
        currentDate = DateUtil.parse("2022-04-08", "yyyy-MM-dd");
        System.out.println("10、自定義格式化轉(zhuǎn)換" + currentDate + "\n");

        // 11、格式化輸出日期
        String format = DateUtil.format(currentDate, "yyyy-MM-dd");
        System.out.println("11、格式化輸出日期, 格式\"yyyy-MM-dd\"):\n" + format + "\n");

        // 12、獲得年的部分
        int year = DateUtil.year(currentDate);
        System.out.println("12、獲得年的部分:\n" + year + "\n");

        // 13、獲得月份,從0開始計數(shù)  +1獲取當(dāng)前月份
        int month = DateUtil.month(currentDate);
        System.out.println("13、獲得月份,從0開始計數(shù)  +1獲取當(dāng)前月份:\n" + month + "\n");

        // 14、獲取某天的開始2020-09-01 00:00:00、結(jié)束時間 2020-09-01 23:59:59
        Date beginOfDay = DateUtil.beginOfDay(currentDate);
        Date endOfDay = DateUtil.endOfDay(currentDate);
        System.out.println("14、獲取今天的開始 00:00:00:\n" + beginOfDay + "\n");
        System.out.println("14、獲取今天的結(jié)束 23:59:59:\n" + endOfDay + "\n");

        // 15、計算偏移后的日期時間 (當(dāng)前時間加兩天)
        Date newDate = DateUtil.offset(currentDate, DateField.DAY_OF_MONTH, 2);
        System.out.println("15、計算偏移后的日期時間 (當(dāng)前時間加兩天):\n" + newDate + "\n");

        // 16、計算日期時間之間的偏移量(date與newDate相差的天數(shù))
        long betweenDay = DateUtil.between(currentDate, newDate, DateUnit.DAY);
        System.out.println("16、計算日期時間之間的偏移量(date與newDate相差的天數(shù)):\n" + betweenDay + "\n");

        System.out.println("========StrUtil.class========");
        // 17、去除字符串的前后綴
        StrUtil.removeSuffix("a.jpg", ".jpg");
        String s = StrUtil.removePrefix("Hello,World", "Hello,");
        System.out.println("17、去除字符串的前后綴:\n " + s + "\n");

        // 18、格式化字符串,占位    你好: 小明
        String template = "你好: {}";
        String str2 = StrUtil.format(template, "小明");
        System.out.println("18、格式化字符串,占位:\n" + str2 + "\n");

        System.out.println("========NumberUtil.class========");
        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);
        //保留兩位小數(shù)
        BigDecimal roundNum = NumberUtil.round(n1, 2);
        String n3 = "1.234";
        //判斷是否為數(shù)字、整數(shù)、浮點(diǎn)數(shù)
        NumberUtil.isNumber(n3);
        NumberUtil.isInteger(n3);
        NumberUtil.isDouble(n3);

        System.out.println("========NumberUtil.class========");

        // 19、bean轉(zhuǎn)map
        Animal animal = new Animal().setName("母雞")
                .setId("93b845146bd2a3ae13ae0da7b65167ap");
        Map<String, Object> map = BeanUtil.beanToMap(animal);
        System.out.println("19、bean轉(zhuǎn)map:\n " + map.get("id") + "\n");

        // 20、map轉(zhuǎn)bean
        Animal mapToBean = BeanUtil.mapToBean(map, Animal.class, false);
        System.out.println("20、map轉(zhuǎn)bean:\n " + mapToBean + "\n");

        // 21、bean屬性拷貝
        Animal newAnimal = new Animal();
        BeanUtil.copyProperties(animal, newAnimal);
        System.out.println("21、bean屬性拷貝:\n" + newAnimal + "\n");

        System.out.println("========CollUtil.class========");

        // 22、數(shù)組轉(zhuǎn)換為列表
        String[] array = new String[]{"a", "b", "c", "d", "e"};
        List<String> list = CollUtil.newArrayList(array);
        String joinStr = CollUtil.join(list, ",");
        System.out.println("22、數(shù)組轉(zhuǎn)字符串時添加連接符號: \n" + joinStr + "\n");
        // 23、將以連接符號分隔的字符串再轉(zhuǎn)換為列表
        List<String> splitList = StrUtil.split(joinStr, ',');
        System.out.println("23、數(shù)組轉(zhuǎn)換為列表: \n");
        splitList.forEach(System.out::println);
        // 25、創(chuàng)建新的Map、Set、List
        System.out.println("25、創(chuàng)建新的Map、Set、List");
        HashMap<Object, Object> newMap = CollUtil.newHashMap();
        HashSet<Object> newHashSet = CollUtil.newHashSet();
        ArrayList<Object> newList = CollUtil.newArrayList();
        // 26、判斷列表是否為空
        CollUtil.isEmpty(list);
        System.out.println("26、判斷列表是否為空");

        System.out.println("========SecureUtil.class========");
        // 27、MD5加密
        String md5Str = SecureUtil.md5("123456");
        System.out.println("27、MD5加密: \n" + md5Str + "\n");

    }

    // 28、生成驗證碼圖片
    public void captchaUtil(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("========CaptchaUtil.class========");
        //生成驗證碼圖片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        try {
            request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
            response.setContentType("image/png");//告訴瀏覽器輸出內(nèi)容為圖片
            response.setHeader("Pragma", "No-cache");//禁止瀏覽器緩存
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




原文鏈接:https://blog.csdn.net/m0_59259076/article/details/124041114

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新