常用工具

常用工具

1.Object转Map

		//获得类的的属性名 数组
        Field[]fields=Object.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                //私有字段必须设置为true
                field.setAccessible(true);
                String name = new String(field.getName());
                //get(Object)返回该所表示的字段的值 Field ,指定的对象上。
                map.put(name, field.get(Object));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

2.Map转Object

	//Map转对象
	public static <T> T parseMap2Object(Map<String, Object> paramMap, Class<T> cls) {
    	return JSONObject.parseObject(JSONObject.toJSONString(paramMap), cls);
	}

3.List去重

	//前提在实体类中重写equals和hashCode方法
    public List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

4.List去重

	private static List filterListByName(List<Map> list) {
        if (null == list || list.size() <= 0) {
            return list;
        } else {
            for (int i = 0; i < list.size(); i++) {
                Map map1 = list.get(i);
                if (map1 != null) {
                    for (int j = i + 1; j < list.size(); j++) {
                        if (map1.get("pm_id").equals(list.get(j).get("pm_id"))) {
                            list.remove(j);
                            j--;
                        }
                    }
                }
            }
            return list;
        }
    }

5.List排序

	Collections.sort(list, new Comparator<Map<String, Double>>() {
            public int compare(Map<String, Double> o1, Map<String, Double> o2) {
                return o2.get("source").compareTo(o1.get("source"));
            }
        });

6.List排序

	Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                int i = o1.getScore() - o2.getScore();
                return i;
            }
        });

7. 计算天数差

	//计算天数差(年月日)
    public int daysCount(String beginTime, String endTime) {

        if (beginTime.equals("") || endTime.equals("")) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String format1 = format.format(new Date());
            beginTime = format1;
            String s = addMonthOrDay(beginTime, 1, 1);
            endTime = s;
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        try {
            long beginTimeL = format.parse(beginTime).getTime();
            long endTimeL = format.parse(endTime).getTime();
            days = (int) ((endTimeL - beginTimeL) / (1000 * 60 * 60 * 24));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days + 1;
    }

8.向后推迟一天或者一个月

	//type=2时向后推迟一个月,type=1时向后推迟一天
    public String addMonthOrDay(String s, int n, Integer type) {
        try {
            SimpleDateFormat sdf = null;
            Calendar cd = null;
            if (type == 1) {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                cd = Calendar.getInstance();
                cd.setTime(sdf.parse(s));
                cd.add(Calendar.DATE, n);//增加一天
            } else {
                sdf = new SimpleDateFormat("yyyy-MM");
                cd = Calendar.getInstance();
                cd.setTime(sdf.parse(s));
                cd.add(Calendar.MONTH, n);//增加一个月
            }
            return sdf.format(cd.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

9.保留两位小数

	public double subDouble(double sou) {
        DecimalFormat df = new DecimalFormat("#.00");
        String source = df.format(sou);
        Double DSource = Double.valueOf(source);
        return DSource;
    }

10.计算两个月之间月份,返回数组

	//计算月份差
    public List<String> getMonth(String beginTime, String endTime) throws Exception {
        Date startDate = null;
        if (beginTime != null && !"".equals(beginTime)) {
            startDate = new SimpleDateFormat("yyyy-MM").parse(beginTime);
        }
        Date endDate = null;
        if (endTime != null && !"".equals(endTime)) {
            endDate = new SimpleDateFormat("yyyy-MM").parse(endTime);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        // 获取开始年份和开始月份
        int startYear = calendar.get(Calendar.YEAR);
        int startMonth = calendar.get(Calendar.MONTH);
        // 获取结束年份和结束月份
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);
        int endMonth = calendar.get(Calendar.MONTH);
        List<String> list = new ArrayList<String>();
        for (int i = startYear; i <= endYear; i++) {
            String date = "";
            if (startYear == endYear) {  //2020
                for (int j = startMonth; j <= endMonth; j++) {  //开始年到结束年
                    if (j < 9) {
                        date = i + "-0" + (j + 1);
                    } else {
                        date = i + "-" + (j + 1);
                    }
                    list.add(date);
                }

            } else {
                if (i == startYear) {
                    for (int j = startMonth; j < 12; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                } else if (i == endYear) {
                    for (int j = 0; j <= endMonth; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                } else {
                    for (int j = 0; j < 12; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                }
            }
        }
        return list;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值