package com.aspire.hbhdc.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
/**
* Hutools工具類測試
*
* @author nanfengxiangbei
* @date 2020/11/16 09:40
*/
public class HuToolsTest {
private static Logger LOGGER = LoggerFactory.getLogger(HuToolsTest.class);
private static void mapUtils() {
//將多個鍵值對加入到Map中
Map<Object, Object> map = MapUtil.of(new String[][]{
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
});
//判斷Map是否為空
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);
}
private static void collectionUtils() {
//數組轉換為列表
String[] array = new String[]{"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);
}
private static void beanUtils() {
PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName("小米");
brand.setShowStatus(0);
//Bean轉Map
Map<String, Object> map = BeanUtil.beanToMap(brand);
//Map轉Bean
PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, true);
System.out.println("Map轉Bean" + mapBrand);
//Bean屬性拷貝
PmsBrand copyBrand = new PmsBrand();
BeanUtil.copyProperties(brand, copyBrand);
System.out.println("Bean屬性拷貝" + copyBrand);
}
private static void numberUtils() {
double n1 = 1.234;
double n2 = 1.234;
BigDecimal n4 = new BigDecimal("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 roundNum2 = NumberUtil.round(n4, 1);
System.out.println("bigDecimal保留小數點幾位:" + roundNum2);
String n3 = "1.2355";
System.out.println("字符串轉bigDecimal:" + NumberUtil.round(NumberUtil.toBigDecimal(n3), 2));
//判斷是否為數字、整數、浮點數
NumberUtil.isNumber(n3);
NumberUtil.isInteger(n3);
NumberUtil.isDouble(n3);
}
private static void getClassPathResource() {
//獲取定義在src/main/resources文件夾中的配置文件
ClassPathResource resource = new ClassPathResource("/processes/generator.properties");
Properties properties = new Properties();
try {
properties.load(resource.getStream());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(properties.get("aaa"));
}
private static void convertDataType() {
//轉換為字符串
int a = 1;
String aStr = Convert.toStr(a);
System.out.println("轉換為字符串:" + aStr);
//轉換為指定類型數組
String[] b = {"1", "2", "3", "4"};
Integer[] bArr = Convert.toIntArray(b);
System.out.println("轉換為指定類型數組:" + bArr);
//轉換為日期對象
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);
String number = "23.5655";
System.out.println(NumberUtil.round(Convert.toBigDecimal(number), 2));
}
private static void DateUtil() {
//Date、long、Calendar之間的相互轉換
//當前時間
Date date = DateUtil.date();
//Calendar轉Date
date = DateUtil.date(Calendar.getInstance());
//時間戳轉Date
date = DateUtil.date(System.currentTimeMillis());
//自動識別格式轉換
String dateStr = "2017-07-31 12:15:10";
date = DateUtil.parse(dateStr);
//自定義格式化轉換
date = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss");
System.out.println("Date轉時間格式:" + date);
//格式化輸出日期
Date current = new Date();
String format = DateUtil.format(current, "yyyy/MM/dd HH:mm:ss");
System.out.println("yyyy-MM-dd HH:ss:mm:" + format);
//獲得年的部分
int year = DateUtil.year(date);
//獲得月份,從0開始計數
int month = DateUtil.month(date);
//獲取某天的開始、結束時間
Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);
System.out.println("今天零點:" + beginOfDay + ",今天12點:" + endOfDay);
//計算偏移后的日期時間
Date newDate = DateUtil.offset(date, DateField.MONTH, -1);
System.out.println("偏移后的日期時間:" + newDate);
//計算日期時間之間的偏移量
long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
System.out.println("計算日期時間之間的偏移量:" + betweenDay);
}
private static void JSONUtil() {
PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName("小米");
brand.setShowStatus(1);
//對象轉化為JSON字符串
String jsonStr = JSONUtil.parse(brand).toString();
System.out.println("jsonUtil parse:{}" + jsonStr);
//JSON字符串轉化為對象
PmsBrand brandBean = JSONUtil.toBean(jsonStr, PmsBrand.class);
System.out.println("jsonUtil toBean:{}" + brandBean);
List<PmsBrand> brandList = new ArrayList<>();
brandList.add(brand);
String jsonListStr = JSONUtil.parse(brandList).toString();
//JSON字符串轉化為列表
brandList = JSONUtil.toList(new JSONArray(jsonListStr), PmsBrand.class);
System.out.println("jsonUtil toList:{}" + brandList);
}
public static void main(String[] args) {
JSONUtil();
//DateUtil();
//convertDataType();
//getClassPathResource();
//numberUtils();
//beanUtils();
//collectionUtils();
//mapUtils();
}
}