hutool工具类常用API整理

0、官网学习地址

1、依赖

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>${hutool.version}</version>
  5. </dependency>

2、工具集

2.1、convert

  • 此工具用于于各种类型数据的转换
  1. // 转换为字符串
  2. @R_618_8237@ = 1;
  3. String aStr = Convert.toStr(a);
  4. // 转换为指定类型数组
  5. String[] b = {"1", "2", "3", "4"};
  6. Integer[] bArr = Convert.toIntArray(b);
  7. // 转换为日期对象
  8. String dateStr = "2017-05-06";
  9. Date date = Convert.toDate(dateStr);
  10. // 转换为列表
  11. String[] strArr = {"a", "b", "c", "d"};
  12. List<String> strList = Convert.toList(String.class, strArr);

2.2、DataUtil

  • 此工具定义了一些操作日期的方法: Date、long、Calendar之间的相互转换
  1. // 当前时间
  2. Date date = DateUtil.date();
  3. // Calendar转Date
  4. date = DateUtil.date(Calendar.geTinstance());
  5. // 时间戳转Date
  6. date = DateUtil.date(System.currentTimeMillis());
  7. // 自动识别格式转换
  8. String dateStr = "2017-03-01";
  9. date = DateUtil.parse(dateStr);
  10. // 自定义格式化转换
  11. date = DateUtil.parse(dateStr, "yyyy-MM-dd");
  12. // 格式化输出日期
  13. String format = DateUtil.format(date, "yyyy-MM-dd");
  14. // 获得年的部分
  15. int year = DateUtil.year(datE);
  16. // 获得月份,从0开始计数
  17. int month = DateUtil.month(datE);
  18. // 获取某天的开始、结束时间
  19. Date beginOfDay = DateUtil.beginOfDay(datE);
  20. Date endOfDay = DateUtil.endOfDay(datE);
  21. // 计算偏移后的日期时间
  22. Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
  23. // 计算日期时间之间的偏移量
  24. long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);

2.3、StrUtil

  • 此工具定义了一些操作字符串的方法
  1. // 判断是否为空字符串
  2. String str = "test";
  3. StrUtil.isEmpty(str);
  4. StrUtil.isnotEmpty(str);
  5. // 去除字符串的前后缀
  6. StrUtil.removeSuffix("a.jpg", ".jpg");
  7. StrUtil.removePrefix("a.jpg", "a.");
  8. // 格式化字符串
  9. String template = "这只是个占位符:{}";
  10. String str2 = StrUtil.format(template, "我是占位符");
  11. LOGGER.info("/strUtil format:{}", str2);

2.4、ClassPathresource

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

2.5、ReflectUtil

  • 此工具是为了反射获取类的方法及创建对象
  1. // 获取某个类的所有方法
  2. Method[] methods = ReflectUtil.getMethods(PmsBrand.class);
  3. // 获取某个类的指定方法
  4. Method method = ReflectUtil.getMethod(PmsBrand.class, "getId");
  5. // 使用反射来创建对象
  6. PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);
  7. // 反射执行对象的方法
  8. ReflectUtil.invoke(pmsBrand, "setId", 1);

2.6、numberUtil

  • 此工具是用于各种类型数字的加减乘除操作及判断类型
  1. double n1 = 1.234;
  2. double n2 = 1.234;
  3. double result;
  4. // 对float、double、Bigdecimal做加减乘除操作
  5. result = numberUtil.add(n1, n2);
  6. result = numberUtil.sub(n1, n2);
  7. result = numberUtil.mul(n1, n2);
  8. result = numberUtil.div(n1, n2);
  9. // 保留两位小数
  10. Bigdecimal roundNum = numberUtil.round(n1, 2);
  11. String n3 = "1.234";
  12. // 判断是否为数字、整数、浮点数
  13. numberUtil.isnumber(n3);
  14. numberUtil.isInteger(n3);
  15. numberUtil.isDouble(n3);

2.7、BeanUtil

  • 此工具是用于Map与JavaBean对象的互相转换以及对象属性的拷贝
  1. PmsBrand brand = new PmsBrand();
  2. brand.setId(1L);
  3. brand.setName("小米");
  4. brand.setShowStatus(0);
  5. // Bean转Map
  6. Map<String, Object> map = BeanUtil.beanToMap(brand);
  7. LOGGER.info("beanUtil bean to map:{}", map);
  8. // Map转Bean
  9. PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, falsE);
  10. LOGGER.info("beanUtil map to bean:{}", mapBrand);
  11. // Bean属性拷贝
  12. PmsBrand copyBrand = new PmsBrand();
  13. BeanUtil.copyProperties(brand, copyBrand);
  14. LOGGER.info("beanUtil copy properties:{}", copyBrand);

2.8、CollUtil

  • 此工具是集合的一些操作
  1. // 数组转换为列表
  2. String[] array = new String[]{"a", "b", "c", "d", "e"};
  3. List<String> list = CollUtil.newArrayList(array);
  4. // 数组转字符串时添加连接符号
  5. String joinstr = CollUtil.join(list, ",");
  6. LOGGER.info("collUtil join:{}", joinstr);
  7. // 将以连接符号分隔的字符串再转换为列表
  8. List<String> splitList = StrUtil.split(joinstr, ',');
  9. LOGGER.info("collUtil split:{}", splitList);
  10. // 创建新的Map、Set、List
  11. HashMap<Object, Object> newMap = CollUtil.newHashMap();
  12. HashSet<Object> newHashSet = CollUtil.newHashSet();
  13. ArrayList<Object> newList = CollUtil.newArrayList();
  14. // 判断列表是否为空
  15. CollUtil.isEmpty(list);

2.9、MapUtil

  • 此工具可用于创建R_670_11845@ap和判断Map是否为null
  1. // 将多个键值对加入到Map中
  2. Map<Object, Object> map = MapUtil.of(new String[][]{
  3. {"key1", "value1"},
  4. {"key2", "value2"},
  5. {"key3", "value3"}
  6. });
  7. // 判断Map是否为空
  8. MapUtil.isEmpty(map);
  9. MapUtil.isnotEmpty(map);

2.10、AnnotationUtil

  • 此工具可用于获取注解和注解中指定的值
  1. // 获取指定类、方法、字段、构造器上的注解列表
  2. Annotation[] AnnotationList = AnnotationUtil.getAnnotations(HutoolController.class, falsE);
  3. LOGGER.info("AnnotationUtil Annotations:{}", AnnotationList);
  4. // 获取指定类型注解
  5. Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
  6. LOGGER.info("AnnotationUtil api value:{}", api.description());
  7. // 获取指定类型注解的值
  8. Object AnnotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, requestMapping.class);

2.11、SecureUtil

  • 此工具用于MD5加密
  1. // MD5加密
  2. String str = "123456";
  3. String md5Str = SecureUtil.md5(str);
  4. LOGGER.info("secureUtil md5:{}", md5Str);

2.12、CaptchaUtil

  • 此工具用于生成图形验证码
  1. // 生成验证码图片
  2. LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
  3. try {
  4. request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
  5. response.setContentType("image/png");//告诉浏览器输出内容为图片
  6. response.setHeader("Pragma", "No-cache");//禁止浏览器缓存
  7. response.setHeader("Cache-Control", "no-cache");
  8. response.setDateHeader("Expire", 0);
  9. lineCaptcha.write(response.getOutputStream());
  10. } catch (IOException E) {
  11. e.printStackTrace();
  12. }
posted @ 2024-02-21 15:53  全琪俊  阅读(147)  评论(0编辑  收藏  举报