Hutool工具类常用方法

maven依赖(jdk8对应版本5 ,jdk7对应版本4)

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

Convert:各种类型数据的转换

//数字转大写
double a = 123456.01;
//输出: 壹拾贰万叁仟肆佰伍拾陆元零壹分
System.out.println(Convert.digitToChinese(a));
//转换为字符串
int i=1;
String aStr = Convert.toStr(i);
//转换为指定类型数组
String[] b = {"1","2","3","4"};
Integer[] bArr = Convert.toIntArray(b);
//转换为日期对象
String dateStr = "2017-05-06";
Date date = Convert.toDate(dateStr);
System.out.println("转换为日期对象: "+date);
//转换为列表
String[] strArr = {"a","b","c","d"};
List<String> strList = Convert.toList(String.class,strArr);
System.out.println("转换为列表: "+strList);

DateUtil: 日期时间工具类

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

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

ClassPathResource:获取classPath下的文件,在Tomcat等容器下classPath一般是WEB-INF/classes

//获取定义在src/main/resources文件夹中的配置文件
ClassPathResource resource = newClassPathResource("generator.properties");
Properties properties = newProperties();
properties.load(resource.getStream());
LOGGER.info("/classPath:{}",properties);

NumberUtil :数字处理工具类

double doublen1 = 1.234;
double doublen2 = 1.234;
//对 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的工具类

PmsBrand brand = newPmsBrand();
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 = newPmsBrand();
BeanUtil.copyProperties(brand,copyBrand);
LOGGER.info("beanUtil copy properties:{}",copyBrand);

CollUtil:集合操作的工具类

//数组转换为列表
String[] array = {"a","b","c","d","e"};
List<String> list = CollUtil.newArrayList(array);
//join:数组转字符串时添加连接符号
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);

MapUtil:Map操作工具类

//将多个键值对加入到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);
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);

SecureUtil:加密解密工具类,可用于MD5加密

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

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

QrCodeUtil:生成二维码

// 生成二维码
QrCodeUtil.generate("https://hutool.cn/",300,300,FileUtil.file("d:/qrcode.jpg"));

其他工具类

Hutool中的工具类很多,可以参考:https://www.hutool.cn/

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

  • 夜莺2517阅读 128,528评论 1赞 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 7,800评论 1赞 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 9,104评论 28赞 54
  • 兔子虽然是枚小硕 但学校的硕士四人寝不够 就被分到了博士楼里 两人一间 在学校的最西边 靠山 兔子的室友身体不好 ...
    待业的兔子阅读 2,895评论 2赞 9

友情链接更多精彩内容