hutool工具类常用API整理
0、官网学习地址
- https://www.hutool.cn/
1、依赖
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency>
2、工具集
2.1、convert
- 此工具用于于各种类型数据的转换
// 转换为字符串@R_618_8237@ = 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转Datedate = DateUtil.date(Calendar.geTinstance());// 时间戳转Datedate = 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转MapMap<String, Object> map = BeanUtil.beanToMap(brand);LOGGER.info("beanUtil bean to map:{}", map);// Map转BeanPmsBrand 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、ListHashMap<Object, Object> newMap = CollUtil.newHashMap();HashSet<Object> newHashSet = CollUtil.newHashSet();ArrayList<Object> newList = CollUtil.newArrayList();// 判断列表是否为空CollUtil.isEmpty(list);
2.9、MapUtil
- 此工具可用于创建R_670_11845@ap和判断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();}