标签:mon copy 相互转换 自定义格式 ack fse imp nta parse
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();
}
}
Hutools工具类测试
标签:mon copy 相互转换 自定义格式 ack fse imp nta parse
原文地址:https://www.cnblogs.com/nanfengxiangbei/p/14183005.html