获取两个月份间的所有月份列表

 

转:

根据日期时间段获取各个月份

 

public static void main(String[] args) throws ParseException {
System.err.println(getDateArrByMonth("201909", "202002"));
}

public static List<String> getDateArrByMonth(String dateStart, String dateEnd) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date startDate = sdf.parse(dateStart);
Date endDate = sdf.parse(dateEnd);
Calendar c = Calendar.getInstance();
List<String> list = new ArrayList<String>();
SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
for (; startDate.getTime() <= endDate.getTime(); ) {
String _startDate = df.format(startDate);
list.add(_startDate);
c.setTime(startDate);
c.add(Calendar.MONTH, 1); //加一个月
startDate = c.getTime();
}
return list;
}
posted @ 2020-09-04 14:43  戈博折刀  阅读(374)  评论(0编辑  收藏  举报