使用java动态获取一年中的年月

private static List getInitMonthMapWithZero(){
		List list = new ArrayList();
		Calendar c = Calendar.getInstance();
		for(int i = 0; i < 12; i ++){
			int k = c.get(Calendar.YEAR);
			int j = c.get(Calendar.MONTH) + 1 - i;
			String date = "";
			if(j >= 1){
				date = k + "-" + (j >= 10 ? "" : "0") + j;
			} else {
				int p = 11 - i;//剩余循环次数
				int m = c.get(Calendar.YEAR) - 1;
				int n = c.get(Calendar.MONTH) + 2 + p;
				date = m + "-" + (n >= 10 ? "" : "0") + n;
			}
			list.add(date);
		}
		return list;
	}

动态获取一年中的年月"yyyy-MM"格式,存入LIst

[2018-07, 2018-06, 2018-05, 2018-04, 2018-03, 2018-02, 2018-01, 2017-12, 2017-11, 2017-10, 2017-09, 2017-08]

你可能感兴趣的:(Java)