java如何遍历当月所有的日期?

转自:https://zhidao.baidu.com/question/360215274.html

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Test {

public static void main(String[] args) {

List<Date> list = getAllTheDateOftheMonth(new Date());

for(Date date: list){
System.out.println(date.toString());
}

}

private static List<Date> getAllTheDateOftheMonth(Date date) {
List<Date> list = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);

int month = cal.get(Calendar.MONTH);
while(cal.get(Calendar.MONTH) == month){
list.add(cal.getTime());
cal.add(Calendar.DATE, 1);
}
return list;
}

}

posted on 2018-03-05 18:27  小甜瓜安东泥  阅读(699)  评论(0编辑  收藏  举报