获取当前时间前几个月的时间集合

   public static List<String> getData(int data){
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.add(Calendar.MONTH, 0);
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        min.add(Calendar.MONTH, -data);
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
        return result;
    }
 public static void main(String[] args) {
        List<String> result = getData(3);

        for (String a:result){
            System.out.println(a);
        }
    }

  

posted @ 2022-05-25 08:40  我是小金人  阅读(100)  评论(0编辑  收藏  举报