获取本月月末到12个月之前的时间(中间相隔12个月)
程序员文章站
2022-05-17 21:27:12
...
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cale = Calendar.getInstance();
cale.set(Calendar.HOUR_OF_DAY, 0);
cale.set(Calendar.MINUTE, 0);
cale.set(Calendar.SECOND, 0);
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
String lastday = format.format(cale.getTime());
Calendar cale1 = Calendar.getInstance();
cale1.set(Calendar.HOUR_OF_DAY, 0);
cale1.set(Calendar.MINUTE, 0);
cale1.add(Calendar.MONTH, 1);
cale1.add(Calendar.YEAR, -1);
cale1.add(Calendar.MONTH, 0);
cale1.set(Calendar.DAY_OF_MONTH, 1);
cale1.set(Calendar.SECOND, 0);
String firstday = format.format(cale1.getTime());
System.out.println("一年前的月份第一天是" + firstday);
System.out.println("一年后的月份最后一天是" + lastday);
执行结果:
一年前的月份第一天是2018-05-01 00:00:00
一年后的月份最后一天是2019-04-30 00:00:00
正好相隔一年,记录以备以后查看。