java中遍历指定的日期
public static void getDate(Integer startDate, Integer endDate) {
Calendar startC = Calendar.getInstance();
startC.set(Calendar.YEAR,Integer.parseInt(String.valueOf(startDate).substring(0,4)));
startC.set(Calendar.MONTH,Integer.parseInt(String.valueOf(startDate).substring(4,6))-1);
startC.set(Calendar.DAY_OF_MONTH,Integer.parseInt(String.valueOf(startDate).substring(6,8)));
startC.set(Calendar.HOUR_OF_DAY,0);
startC.set(Calendar.MINUTE,0);
startC.set(Calendar.SECOND,0);
Calendar endC = Calendar.getInstance();
endC.set(Calendar.YEAR,Integer.parseInt(String.valueOf(endDate).substring(0,4)));
endC.set(Calendar.MONTH,Integer.parseInt(String.valueOf(endDate).substring(4,6))-1);
endC.set(Calendar.DAY_OF_MONTH,Integer.parseInt(String.valueOf(endDate).substring(6,8)));
endC.set(Calendar.HOUR_OF_DAY,0);
endC.set(Calendar.MINUTE,0);
endC.set(Calendar.SECOND,0);
for(Calendar cl=startC;cl.getTime().getTime()<=endC.getTime().getTime();
cl.add(Calendar.DAY_OF_MONTH,1)) {
Integer year = cl.get(Calendar.YEAR);
Integer month = cl.get(Calendar.MONTH) + 1;
Integer clDay = cl.get(Calendar.DAY_OF_MONTH);
String mon = null;
if (month < 10) {
mon = "0"+String.valueOf(month);
}else{
mon=String.valueOf(month);
}
String day = null;
if (clDay < 10) {
day = "0"+String.valueOf(clDay);
}else{
day =String.valueOf(clDay);
}
String dateRes = String.valueOf(String.valueOf(year) + mon + day);
System.out.println(dateRes);
}
}
03-02
1079