欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取最近几个月的月份

程序员文章站 2022-05-15 09:54:05
...

获取最近几个月的月份

// An highlighted block
private List<String> getLast6Month(){
        List<String> rList = new ArrayList<>();
        try{
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -5);
        String before_six = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH);//六个月前
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.setTime(sdf.parse(before_six));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        max.setTime(sdf.parse(sdf.format(new Date())));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        Calendar curr = min;
        while (curr.before(max)) {
            rList.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return rList;
    }
相关标签: abc