hutool工具类常用API整理
0、官网学习地址
1、依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
2、工具集
2.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.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);
2.3、StrUtil
- 此工具定义了一些操作字符串的方法
// 判断是否为空字符串
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);
2.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);
2.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);
2.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);
2.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);
2.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);
2.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);
2.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);
2.11、SecureUtil
- 此工具用于MD5加密
// MD5加密
String str = "123456";
String md5Str = SecureUtil.md5(str);
LOGGER.info("secureUtil md5:{}", md5Str);
2.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();
}
hutool工具类常用API整理的更多相关文章
- StringUtils工具类常用api <转>
该工具类是用于操作Java.lang.String类的. StringUtils类与String类的区别在于:此类是null安全的,即如果输入参数String为null,则不会抛出NullPointe ...
- Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源,BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 各种后台管理系统
Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 家庭理财系统 各种后 ...
- Hutool工具类之HttpUtil使用Https
关于Hutool工具类之HttpUtil如何使用可以参考官方文档Hutool之HttpUtil 其实使用Http和Https使用的方式是一样的. 建议大家可以看看HttpUtil的源码,感觉设计的挺不 ...
- Java之String类常用API
目录 Java之String类常用API char chatAt(int index) int length() char[] toCharArray() String(char value[]) S ...
- 【Hutool】Hutool工具类之日期时间工具——DateUtil
一.用于取代Date对象的DateTime对象 再也不用Date SimpleDateFormat Calendar之间倒腾来倒腾去了!日期创建-获取-操作一步到位! 如果JDK版本更新到了8及以上, ...
- 【Hutool】Hutool工具类之随机工具——RandomUtil
commons-lang中对应也有RanddomUtils.RandomStringUtils 直接从类结构开始入手: 基本都是见名知意了,就不一一展开:点开源码可以看到算是比较通俗易懂的对Rando ...
- 【Hutool】Hutool工具类之Http工具——HttpUtil
最简单最直接的上手可以参见参考文档:http://hutool.mydoc.io/?t=216015 Http协议的介绍,请参考web随笔:http://www.cnblogs.com/jiang ...
- 使用hutool工具类进行导出
引入依赖为: <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</ ...
- Go语言中使用K8s API及一些常用API整理
Go Client 在进入代码之前,理解k8s的go client项目是对我们又帮助的.它是k8s client中最古老的一个,因此具有很多特性. Client-go 没有使用Swagger生成器,就 ...
随机推荐
- 算法 | Java 常见排序算法(纯代码)
目录 汇总 1. 冒泡排序 2. 选择排序 3. 插入排序 4. 快速排序 5. 归并排序 6. 希尔排序 6.1 希尔-冒泡排序(慢) 6.2 希尔-插入排序(快) 7. 堆排序 8. 计数排序 9 ...
- Oracle 11g中查询CPU占有率高的SQL
oracle版本:oracle11g 背景:今天在Linux中的oracle服务上,运用top命令发现许多进程的CPU占有率是100%. 操作步骤: 以进程PID:7851为例 执行以下语句: 方法一 ...
- zookeeper可视化WEB工具(zkui)搭建与配置
前提:zookeeper 可视化WEB工具zkui依赖java环境,因此需要安装jdk,同时zkui源码要Maven编译,需要安装apache-maven. JDK下载地址:https://www.o ...
- 集合 copy
#集合的创建 # set = set(["barry",1,2]) # print(set) # set1 = {1,2,3} #集合的增 # set1 = {'alex','wu ...
- SQL语句优化、mysql不走索引的原因、数据库索引的设计原则
SQL语句优化 1 企业SQL优化思路 1.把一个大的不使用索引的SQL语句按照功能进行拆分 2.长的SQL语句无法使用索引,能不能变成2条短的SQL语句让它分别使用上索引. 3.对SQL语句功能的拆 ...
- python练习册 每天一个小程序 第0006题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...
- NLP 自然语言处理实战
前言 自然语言处理 ( Natural Language Processing, NLP) 是计算机科学领域与人工智能领域中的一个重要方向.它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和 ...
- linux管理用户(组)与相关问题处理
相关联文件如下: /etc/passwd/etc/shadow/etc/group ================================= [切换当前用户为root]sudo -i [创建 ...
- 在JAVA中如何跳出当前的多重嵌套循环?
在Java中,要想跳出多重循环,可以在外面的循环语句前定义一个标号,然后在里层循环体的代码中使用带有标号的break语句,即可跳出外层循环.
- spring-boot-learning-REST风格网站
什么是REST风格: Representational State Transfer :表现层状态转换,实际上是一种风格.标准,约定 首先需要有资源才能表现, 所以第一个名词是" 资源&qu ...