java 获取目标时间到当前时间中间的月份和每月最大时间
话不多说自己上代码:
Date firstDate = DateUtil.formate("2018-01-01", "yyyy-MM-dd"); Date currDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(currDate); int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, lastDay); currDate = calendar.getTime(); calendar.setTime(firstDate); Date itemDate = null; while (calendar.getTime().before(currDate)) { lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, lastDay); itemDate = calendar.getTime(); System.out.println(DateUtil.formate(itemDate, "yyyy-MM-dd")); calendar.add(Calendar.MONTH, 1); }
结果: