hutool工具常用API

2023-11-09


依赖

	<dependency>
		 <groupId>cn.hutool</groupId>
		 <artifactId>hutool-all</artifactId>
		 <version>${hutool.version}</version>
	</dependency>

工具API

1、convert

  • 此工具用于各种类型数据的转换

	// 转换为字符串
	int a = 1;
	String aStr = Convert.toStr(a);

	// 转换为指定类型数组
	String[] b = {"1", "2", "3", "4"};
	Integer[] bArr = Convert.toIntArray(b);

	// 转换为日期对象
	String dateStr = "2017-05-06";
	Date date = Convert.toDate(dateStr);

	// 转换为列表
	String[] strArr = {"a", "b", "c", "d"};
	List<String> strList = Convert.toList(String.class, strArr);

2、DataUtil

  • 内容缩进此工具定义了一些操作日期的方法: 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";
	date = DateUtil.parse(dateStr);

	// 自定义格式化转换
	date = DateUtil.parse(dateStr, "yyyy-MM-dd");

	// 格式化输出日期
	String format = DateUtil.format(date, "yyyy-MM-dd");

	// 获得年的部分
	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);

3、StrUtil

  • 此工具定义了一些操作字符串的方法
    hutool工具下的 strUtil.isNotBlank()
    值:null 或者 “” 或者 “\t\n” 均为false
    值:“abc” 结果才为true
	// 判断是否为空字符串
	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, "我是占位符");
	LOGGER.info("/strUtil format:{}", str2);

4、ClassPathResource

  • 此工具是获取ClassPath下的文件,在Tomcat等容器中,ClassPath一般为:WEB-INFO/classes
	// 获取定义在src/main/resources文件夹中的配置文件
	ClassPathResource resource = new ClassPathResource("generator.properties");
	Properties properties = new Properties();
	properties.load(resource.getStream());
	LOGGER.info("/classPath:{}", properties);

5、ReflectUtil

  • 此工具是为了反射获取类的方法及创建对象

	// 获取某个类的所有方法
	Method[] methods = ReflectUtil.getMethods(PmsBrand.class);

	// 获取某个类的指定方法
	Method method = ReflectUtil.getMethod(PmsBrand.class, "getId");

	// 使用反射来创建对象
	PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);

	// 反射执行对象的方法
	ReflectUtil.invoke(pmsBrand, "setId", 1);

6、NumberUtil

  • 此工具是用于各种类型数字的加减乘除操作及判断类型

	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);

7、BeanUtil

  • 此工具是用于Map与JavaBean对象的互相转换以及对象属性的拷贝

	PmsBrand brand = new PmsBrand();
	brand.setId(1L);
	brand.setName("小米");
	brand.setShowStatus(0);

	// Bean转Map
	Map<String, Object> map = BeanUtil.beanToMap(brand);
	LOGGER.info("beanUtil bean to map:{}", map);

	// Map转Bean
	PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
	LOGGER.info("beanUtil map to bean:{}", mapBrand);

	// Bean属性拷贝
	PmsBrand copyBrand = new PmsBrand();
	BeanUtil.copyProperties(brand, copyBrand);
	LOGGER.info("beanUtil copy properties:{}", copyBrand);

8、CollUtil

  • 此工具是集合的一些操作

	// 数组转换为列表
	String[] array = new String[]{"a", "b", "c", "d", "e"};
	List<String> list = CollUtil.newArrayList(array);

	// 数组转字符串时添加连接符号
	String joinStr = CollUtil.join(list, ",");
	LOGGER.info("collUtil join:{}", joinStr);

	// 将以连接符号分隔的字符串再转换为列表
	List<String> splitList = StrUtil.split(joinStr, ',');
	LOGGER.info("collUtil split:{}", splitList);

	// 创建新的Map、Set、List
	HashMap<Object, Object> newMap = CollUtil.newHashMap();
	HashSet<Object> newHashSet = CollUtil.newHashSet();
	ArrayList<Object> newList = CollUtil.newArrayList();

	// 判断列表是否为空
	CollUtil.isEmpty(list);

9、MapUtil

  • 此工具可用于创建Map和判断Map是否为null

	// 将多个键值对加入到Map中
	Map<Object, Object> map = MapUtil.of(new String[][]{
		{"key1", "value1"},
		{"key2", "value2"},
		{"key3", "value3"}
	});

	// 判断Map是否为空
	MapUtil.isEmpty(map);
	MapUtil.isNotEmpty(map);

10、AnnotationUtil

  • 此工具可用于获取注解和注解中指定的值

	// 获取指定类、方法、字段、构造器上的注解列表
	Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class, false);
	LOGGER.info("annotationUtil annotations:{}", annotationList);

	// 获取指定类型注解
	Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
	LOGGER.info("annotationUtil api value:{}", api.description());

	// 获取指定类型注解的值
	Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);

11、SecureUtil

  • 此工具用于MD5加密

	// MD5加密
	String str = "123456";
	String md5Str = SecureUtil.md5(str);
	LOGGER.info("secureUtil md5:{}", md5Str);

12、CaptchaUtil

  • 此工具用于生成图形验证码

	// 生成验证码图片
	LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
	try {
		request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
		response.setContentType("image/png");//告诉浏览器输出内容为图片
		response.setHeader("Pragma", "No-cache");//禁止浏览器缓存
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);
		lineCaptcha.write(response.getOutputStream());
	} catch (IOException e) {
		e.printStackTrace();
	}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hutool工具常用API 的相关文章

随机推荐

  • 性能测试_Day_10(负载测试-获得最大可接受用户并发数)

    目录 如何理解负载测试 如何实现负载测试 jpgc Standard Set插件安装 jpgc Standard Set使用方法 负载测试分析指标 获得最大可接受用户并发数 区间值 负载测试分析指标 获得最大可接受用户并发数 真实值 负载测
  • 阅读论文《Deep Bilateral Learning for Real-Time Image Enhancement》

    这是2017 siggraph的一篇论文 寒假boss让我看这篇论文我没怎么看懂 最近在公司实习 发现该论文的成果已经移到手机端上了 效果还非常不错 这里我重新温习了一下这篇论文 发现有许多可以借鉴的地方 是一篇非常不错的论文 这里重新叙述
  • 我碰到avs错误

    1 写好的avs脚本用播发器不能播放 并且报unexpected chatacter 错误 解决办法 1 尽管avs支持汉语文件路径 但是仍要确认标点符号是否为英文状态下 2 将AVS脚本用记事本打开 重新存为并把编码格式修改成ASNI格式
  • 数值计算方法python实现

    包括 泰勒级数展开 差分逼近微分 二分法求解 试位法求解 迭代法求根 牛顿法求根 正割法 贝尔斯托法多项式求跟 多项式回归 牛顿差商插值 拉格朗日插值法 三次样条插值法 二次样条插值法 高斯消元法 求解线性代数方程组 代码在我的github
  • 事件循环与线程 一

    初次读到这篇文章 译者感觉如沐春风 深刻体会到原文作者是花了很大功夫来写这篇文章的 文章深入浅出 相信仔细读完原文或下面译文的读者一定会有收获 由于原文很长 原文作者的行文思路是从事件循环逐渐延伸到线程使用的讨论 译者因时间受限 暂发表有关
  • SnowFlake 算法

    SnowFlake 算法 1 介绍 是 Twitter 开源的分布式 id 生成算法 核心思想 使用一个 64 bit 的 long 型的数字作为全局唯一 id 2 结构 0 0001000000 0000010000 0001000100
  • KVM架构与原理详解

    1 KVM架构 KVM 基本上有两个组件构成 1 kvm 驱动 现在已经是Linux内核的一个模块了 它的作用主要是负责虚拟机的创建 虚拟内存的分配 虚拟CPU寄存器的读写和虚拟cpu的运行 2 另一个组件是 Qemu QEMU是一个通用的
  • Wsl2 Ubuntu18.04图形化界面,亲测成功

    Wsl2 Ubuntu18 04图形化界面 亲测成功 Windows端 Linux端 最后 抖抖索索搞了两天 差点Windows系统都重装 终于搞成功了 参考文献 一定要看 非常感谢这个哥们 成功搞出来了 Windows端 powershe
  • ThreadLoacl

    目录 三 ThreadLoacl 基础 二 InheritableThreadLocal 三 TransmittableThreadLocal 三 ThreadLoacl 基础 在Java的多线程编程中 为保证多个线程对共享变量的安全访问
  • 数据库配置时useUnicode=true&characterEncoding=UTF-8

    数据库连接时经常会写到 jdbc url jdbc mysql localhost 3306 db1 useUnicode true characterEncoding UTF 8 添加的作用是 指定字符的编码 解码格式 例如 mysql数
  • mvvm设计模式总结

    要了解mvvm 首先要了解mvc和mvp 我们也先简单说一下mvc和mvp MVC MVC全名是Model View Controller 是模型 model 视图 view 控制器 controller 的缩写 一种软件设计典范 用一种业
  • HyperLedger Fabric实战(一):基础环境构建

    1 简介 本文档说明了HyperLedger Fabric 1 4 0版本的区块链网络搭建所需的基本环境组件以及安装流程 最后再记录了安装过程中可能会遇到的一些问题 采用的操作系统为ubuntu 18 04 具有参考价值的网站 Hyperl
  • PAT初级1015德才论(C++)

    PAT初级1015德才论 C 代码 include
  • FreeRtos队列,队列集合学习使用

    我们都知道队列可以进行消息的管理 比如在一个task中发消息 另一个task监听队列中是否有消息 这样比读flag的效率要高很多 更好的利用资源 一 介绍一下接下来需要使用到的接口函数 创建队列 使用的是xQueueCreate uxQue
  • Redis-入门与springboot整合

    Redis入门 一 Redis基础命令 二 常用数据类型 1 String类型 2 List类型 3 Set集合 4 hash集合 5 Zset集合 三 Redis发布和订阅 四 新数据类型 1 Bitmaps 2 HyperLogLog
  • Java常用类:System类

    文章目录 System类概述 1 arraycopy 方法 概述 语法 举例 2 currentTimeMillis 方法 概述 语法 举例 3 gc 方法 概述 语法 举例 4 exit int status 方法 概述 语法 举例 Sy
  • openwrt18.06.4配置strongswan对接山石网科(hillstone)记录①

    首先感谢https blog csdn net d9394952 article details 90734469 原贴作者 摸索了一个礼拜 将过程记录如下 首先将路由器连上网 更新opkg root OpenWrt ping www ba
  • aivms--CentOS7.6安装/JDK1.8/ThingsBoard CE /PostgreSQL

    先决条件 yum install y nano wget yum install y https dl fedoraproject org pub epel epel release latest 7 noarch rpm 1 安装JDK8
  • Catowice City【Codeforces 1248 F】【BFS】

    Codeforces Round 594 Div 2 F 一开始是听闻有人说这是一道Tarjan好题 然后就点进来做了 但是想来想去 却想了个另类的法子 我们可以看到 如果N个人都要选择的话 那么每个人都只能是审判者 或者是参赛者 所以 我
  • hutool工具常用API

    hutool工具常用API 依赖 工具API 1 convert 2 DataUtil 3 StrUtil 4 ClassPathResource 5 ReflectUtil 6 NumberUtil 7 BeanUtil 8 CollUt