我試圖檢索當週IE中的日期:週一至週日。然後我有兩個按鈕,上一個和下一個應該檢索下一個和上一個星期的日期。這應該讓我可以無限期地輪換一年中的幾周。Android日曆獲得當前周,前幾周和下週
- 上一頁周:2017/05/8-2017/5月14日
- 加載周:2017/05/15-2017/5月21日
- 下週:2017/05/22-2017/05/28
代碼載入中間日期Mon-Sun。當我點擊下一個按鈕時,它會將我帶到下一週的下一天。當我從這裏點擊Previous時,它會跳過初始加載的星期並跳轉到下一個星期的日期。當我以前一個到下一個去另一個方向時,會發生同樣的情況。當我繼續點擊下一個和上一個按鈕時,他們不會繼續加載新的周。
我在做什麼錯?
初始加載代碼:
DateFormat format = new SimpleDateFormat("M-dd");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
下週代碼:
DateFormat format = new SimpleDateFormat("M-dd");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.add(Calendar.DAY_OF_MONTH, 7);
days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
上一頁周碼:
DateFormat format = new SimpleDateFormat("M-dd");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.add(Calendar.DAY_OF_MONTH, -7);
days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
解決方案:
Calendar calendar; // Stick this at the start of your code
初始加載代碼:
DateFormat format = new SimpleDateFormat("M-dd");
calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
下週代碼:
DateFormat format = new SimpleDateFormat("M-dd");
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.add(Calendar.DAY_OF_MONTH, 7);
days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
上一頁周碼:
DateFormat format = new SimpleDateFormat("M-dd");
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.add(Calendar.DAY_OF_MONTH, -7);
days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
*「我在做什麼錯?」*你不記得你在哪裏。您的「下週代碼」將在下週總是獲得**,相對於今天**,而不是相對於當前顯示的星期。 – Andreas
你會如何解決它? – BrownT