1 public static void printWeeks() { 2 SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd"); 3 Calendar calendar = Calendar.getInstance(); 4 calendar.set(Calendar.DATE, 1); 5 int month = calendar.get(Calendar.MONTH); 6 int count = 0; 7 while (calendar.get(Calendar.MONTH) == month) { 8 if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { 9 StringBuilder builder = new StringBuilder(); 10 builder.append("week:"); 11 builder.append(++count); 12 builder.append(" ("); 13 builder.append(format.format(calendar.getTime())); 14 builder.append(" - "); 15 calendar.add(Calendar.DATE, 6); 16 builder.append(format.format(calendar.getTime())); 17 builder.append(")"); 18 System.out.println(builder.toString()); 19 } 20 calendar.add(Calendar.DATE, 1); 21 } 22 }
打印结果: