java获取当前时间前12个月内的年月信息

工作中时可能会遇到需要循环查询一年相关历史表的业务;
如何获取12个年月信息:

	@Test
	public void mian() throws Exception{
		Calendar c = Calendar.getInstance();
		//取当前时间12个月内的年月
		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;
			}
			System.out.println(date);
		}
	}

在这里插入图片描述


版权声明:本文为weixin_46120673原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。