public class Test {
private Calendar calendar;
public String[] getCurrentWeek() {
this.calendar = Calendar.getInstance();
this.calendar.setFirstDayOfWeek(Calendar.SUNDAY);
this.calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return getNextWeek();
}
public String[] getNextWeek() {
DateFormat format = new SimpleDateFormat("M-dd");
String[] days = new String[7];
for (int i = 0; i < 7; i++) {
days[i] = format.format(this.calendar.getTime());
this.calendar.add(Calendar.DATE, 1);
}
return days;
}
public String[] getPreviousWeek() {
this.calendar.add(Calendar.DATE, -14);
return getNextWeek();
}
public static void main(String[] args) {
Test t = new Test();
System.out.println("Current : " + Arrays.toString(t.getCurrentWeek()));
System.out.println("Previous: " + Arrays.toString(t.getPreviousWeek()));
System.out.println("Previous: " + Arrays.toString(t.getPreviousWeek()));
System.out.println("Next : " + Arrays.toString(t.getNextWeek()));
System.out.println("Next : " + Arrays.toString(t.getNextWeek()));
}
}
出力
Current : [5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27]
Previous: [5-14, 5-15, 5-16, 5-17, 5-18, 5-19, 5-20]
Previous: [5-07, 5-08, 5-09, 5-10, 5-11, 5-12, 5-13]
Next : [5-14, 5-15, 5-16, 5-17, 5-18, 5-19, 5-20]
Next : [5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27]
は、第一の表示の末尾に '' calendar'の値ではありません5-28'?そしてあなたは '5-28'(これはあなたを5-21にします)から引き算して7回実行します。あなたは再び実行し、5-21から引きますか?だから問題は、週の始まりを別の 'Calendar'オブジェクトに保存するべきですか? – KevinO