要使用日历和 WEEK_OF_YEAR 进行计算:
int startWeek;
int finishWeek;
int diff;
SimpleDateFormat sdf;
Calendar cal;
Calendar startCountingCal;
Date startDate;
Date finishDate;
String startDateS = "01/01/2013";
String finishDateS = "01/05/2013";
sdf = new SimpleDateFormat("dd/MM/yyyy");
startDate = sdf.parse(startDateS);
finishDate = sdf.parse(finishDateS);
cal = Calendar.getInstance();
cal.setTime(startDate);
startWeek = cal.get(Calendar.WEEK_OF_YEAR);
cal.setTime(finishDate);
finishWeek = cal.get(Calendar.WEEK_OF_YEAR);
diff = finishWeek - startWeek;
startCountingCal = Calendar.getInstance();
startCountingCal.setTime(startDate);
for (int i = 0; i < diff; i++) {
if (i==0) {
System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));
startCountingCal.add(Calendar.DATE, 7);
System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));
} else {
System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));
startCountingCal.add(Calendar.DATE, 7);
System.out.println("WEEK " + i + " start: " + sdf.format(startCountingCal.getTime()));
}
}
输出:
WEEK 0 start: 01/01/2013
WEEK 0 start: 08/01/2013
WEEK 1 start: 08/01/2013
WEEK 1 start: 15/01/2013
WEEK 2 start: 15/01/2013
WEEK 2 start: 22/01/2013
WEEK 3 start: 22/01/2013
WEEK 3 start: 29/01/2013
....
etc.
希望能帮助到你!