依赖
<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();
date = DateUtil.date(Calendar.getInstance());
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);
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
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;
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);
Map<String, Object> map = BeanUtil.beanToMap(brand);
LOGGER.info("beanUtil bean to map:{}", map);
PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
LOGGER.info("beanUtil map to bean:{}", mapBrand);
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);
HashMap<Object, Object> newMap = CollUtil.newHashMap();
HashSet<Object> newHashSet = CollUtil.newHashSet();
ArrayList<Object> newList = CollUtil.newArrayList();
CollUtil.isEmpty(list);
9、MapUtil
Map<Object, Object> map = MapUtil.of(new String[][]{
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
});
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
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();
}