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;
}
}
不积跬步无以至千里,不积小流无以成江海