maven依賴(jdk8對(duì)應(yīng)版本5 而jdk7對(duì)應(yīng)版本4)
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.1</version>
</dependency>
栗子:(Convert)
//數(shù)字轉(zhuǎn)大寫
double a = 123456.01;
System.out.println(Convert.digitToChinese(a));
//輸出: 壹拾貳萬叁仟肆佰伍拾陸元零壹分
//轉(zhuǎn)換為字符串
int i = 1;
String aStr = Convert.toStr(a);
//轉(zhuǎn)換為指定類型數(shù)組
String[] b = {"1", "2", "3", "4"};
Integer[] bArr = Convert.toIntArray(b);
//轉(zhuǎn)換為日期對(duì)象
String dateStr = "2017-05-06";
Date date = Convert.toDate(dateStr);
System.out.println("轉(zhuǎn)換為日期對(duì)象: "+date);
//轉(zhuǎn)換為列表
String[] strArr = {"a", "b", "c", "d"};
List<String> strList = Convert.toList(String.class, strArr);
System.out.println("轉(zhuǎn)換為列表: "+strList);
DateUtil: 日期時(shí)間工具類警检,定義了一些常用的日期時(shí)間操作方法养葵。
//Date院峡、long荣倾、Calendar之間的相互轉(zhuǎn)換
//當(dāng)前時(shí)間
Date date = DateUtil.date();
//Calendar轉(zhuǎn)Date
date = DateUtil.date(Calendar.getInstance());
//時(shí)間戳轉(zhuǎn)Date
date = DateUtil.date(System.currentTimeMillis());
//自動(dòng)識(shí)別格式轉(zhuǎn)換
String dateStr = "2017-03-01";
date = DateUtil.parse(dateStr);
//自定義格式化轉(zhuǎn)換
date = DateUtil.parse(dateStr, "yyyy-MM-dd");
//格式化輸出日期
String format = DateUtil.format(date, "yyyy-MM-dd");
//獲得年的部分
int year = DateUtil.year(date);
//獲得月份,從0開始計(jì)數(shù) +1獲取當(dāng)前月份
int month = DateUtil.month(date);
//獲取某天的開始2020-09-01 00:00:00思灌、結(jié)束時(shí)間 2020-09-01 23:59:59
Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);
//計(jì)算偏移后的日期時(shí)間 (當(dāng)前時(shí)間加兩天)
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
//計(jì)算日期時(shí)間之間的偏移量(date與newDate相差的天數(shù))
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.");
//格式化字符串 這只是個(gè)占位符:我是占位符
String template = "這只是個(gè)占位符:{}";
String str2 = StrUtil.format(template, "我是占位符");
LOGGER.info("/strUtil format:{}", str2);
ClassPathResource:獲取classPath下的文件泰偿,在Tomcat等容器下熄守,classPath一般是WEB-INF/classes。
//獲取定義在src/main/resources文件夾中的配置文件
ClassPathResource resource = new ClassPathResource("generator.properties");
Properties properties = new Properties();
properties.load(resource.getStream());
LOGGER.info("/classPath:{}", properties);
NumberUtil :數(shù)字處理工具類耗跛,可用于各種類型數(shù)字的加減乘除操作及判斷類型裕照。
double n1 = 1.234;
double n2 = 1.234;
double result;
//對(duì)float、double调塌、BigDecimal做加減乘除操作
result = NumberUtil.add(n1, n2);
result = NumberUtil.sub(n1, n2);
result = NumberUtil.mul(n1, n2);
result = NumberUtil.div(n1, n2);
//保留兩位小數(shù)
BigDecimal roundNum = NumberUtil.round(n1, 2);
String n3 = "1.234";
//判斷是否為數(shù)字晋南、整數(shù)、浮點(diǎn)數(shù)
NumberUtil.isNumber(n3);
NumberUtil.isInteger(n3);
NumberUtil.isDouble(n3);
BeanUtil:JavaBean的工具類羔砾,可用于Map與JavaBean對(duì)象的互相轉(zhuǎn)換以及對(duì)象屬性的拷貝负间。
PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName("華為");
brand.setShowStatus(0);
//Bean轉(zhuǎn)Map
Map<String, Object> map = BeanUtil.beanToMap(brand);
LOGGER.info("beanUtil bean to map:{}", map);
//Map轉(zhuǎn)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);
CollUtil:集合操作的工具類聪铺,定義了一些常用的集合操作封救。
//數(shù)組轉(zhuǎn)換為列表
String[] array = new String[]{"a", "b", "c", "d", "e"};
List<String> list = CollUtil.newArrayList(array);
//join:數(shù)組轉(zhuǎn)字符串時(shí)添加連接符號(hào)
String joinStr = CollUtil.join(list, ",");
LOGGER.info("collUtil join:{}", joinStr);
//將以連接符號(hào)分隔的字符串再轉(zhuǎn)換為列表
List<String> splitList = StrUtil.split(joinStr, ',');
LOGGER.info("collUtil split:{}", splitList);
//創(chuàng)建新的Map、Set留储、List
HashMap<Object, Object> newMap = CollUtil.newHashMap();
HashSet<Object> newHashSet = CollUtil.newHashSet();
ArrayList<Object> newList = CollUtil.newArrayList();
//判斷列表是否為空
CollUtil.isEmpty(list);
MapUtil:Map操作工具類态秧,可用于創(chuàng)建Map對(duì)象及判斷Map是否為空董虱。
//將多個(gè)鍵值對(duì)加入到Map中
Map<Object, Object> map = MapUtil.of(new String[][]{
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
});
//判斷Map是否為空
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);
AnnotationUtil:注解工具類,可用于獲取注解與注解中指定的值申鱼。
//獲取指定類愤诱、方法、字段润讥、構(gòu)造器上的注解列表
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:驗(yàn)證碼工具類楚殿,可用于生成圖形驗(yàn)證碼撮慨。
//生成驗(yàn)證碼圖片
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
try {
request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
response.setContentType("image/png");//告訴瀏覽器輸出內(nèi)容為圖片
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中的工具類很多竿痰,可以參考:https://www.hutool.cn/