003-常用Utils-1

一、类型转换

1.Map 与 Bean 之间的转换
  • Fastjson用法案例
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2</artifactId>
    <version>2.0.51</version>
</dependency>
	@Data
	public class TestClass {
   
	
	    private String name;
	
	    private int age;
	
	    private String address;
	
	}
	
	@Test
    public void test2(){
   

        Map<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",18);
        map.put("address","北京");

        TestClass testClass = JSONObject.parseObject(JSONUtil.toJsonStr(map), TestClass.class);
        System.out.println(testClass);

        Map<String, Object> jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(testClass));
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("name"));

    }
  • Hutool用法案例
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.25</version>
</dependency>
	@Test
    public void test1(){
   
        Map<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",18);
        map.put("address","北京");

        TestClass testClass = JSONUtil.toBean(JSONUtil.toJsonStr(map), TestClass.class);
        System.out.println(testClass);

        Map<String, Object> jsonObject = JSONUtil.parseObj(JSONUtil.toJsonStr(testClass));
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("name"));

        Map<String, Object> params = BeanUtil.beanToMap(testClass);
        System.out.println(params);
    }

二、日期工具

1.获取两个时间节点之间的月份列表
  • 原始版本
/**
 * 获取两个时间节点之间的月份列表
 * 原始版本
 * @param startTime
 * @param endTime
 * @return
 */
public static List<String> getMonthListByDate(String startTime, String endTime) {
   
    ArrayList<String> result = new ArrayList<>();

    try {
   
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();

        startDate.setTime(sdf.parse(startTime));
        startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), 1);

        endDate.setTime(sdf.parse(endTime));
        endDate.set(endDate.get(Calendar.YEAR), endDate.get(Calendar.MONTH), 2);

        Calendar curr = startDate;
        while (curr.before(endDate)) {
   
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
    } catch (ParseException e) {
   
        e.printStackTrace();
    }

    return result;
}
  • 升级版
/**
 * 获取两个时间节点之间的月份列表
 * 升级版
 * @param startTime
 * @param endTime
 * @return
 */
public static List<String> getMonthListByDate(String startTime, String endTime) {
   
    if(StrUtil.isBlank(startTime) || StrUtil.isBlank(endTime)) {
   
        return Collections.emptyList();
    }
    ArrayList<String> result = new ArrayList<>();
    Date startDate = DateUtil.parse(startTime);
    Date endDate = DateUtil.parse(endTime);
    while (startDate.compareTo(endDate)<=0) {
   
        result.add(DateUtil.format(startDate,DatePattern.NORM_MONTH_PATTERN));
        startDate = DateUtil.offsetMonth(startDate,1);
    }
    return result;
}
2.判断当前时间是否在某个时间范围内
  • 原始版本
/**
 * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
 *
 * @param nowTime 当前时间
 * @param startTime 开始时间
 * @param endTime 结束时间
 * @return
 */
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
   
    if(nowTime == null || startTime == null || endTime == null){
   
        return false;
    }
    
    if (nowTime.getTime() == startTime.getTime()
            || nowTime.getTime() == endTime.getTime()) {
   
        return true;
    }

    Calendar date = Calendar.getInstance();
    date.setTime(nowTime);

    Calendar begin = Calendar.getInstance();
    begin.setTime(startTime);

    Calendar end = Calendar.getInstance();
    end.setTime(endTime);

    if (date.after(begin) && date.before(end)) {
   
        return true;
    } else {
   
        return false;
    }
}
  • 升级版
/**
 * 判断当前时间是否在[startTime, endTime]区间
 * 注意时间格式要一致
 * 升级版
 * @param nowTime 当前时间
 * @param startTime 开始时间
 * @param endTime 结束时间
 * @return
 */
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
   
    if(nowTime == null || startTime == null || endTime == null){
   
        return false;
    }

    if (nowTime.getTime() == startTime.getTime()
            || nowTime.getTime() == endTime.getTime()) {
   
        return true;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值