我想点击按钮显示一周中的所有日子。动作将是这样的,例如假设月份是FEB - 2013,所以我第一次点击按钮这一天我想显示。
3/11/2013, 4/11/2013, 5/11/2013, 6/11/2013, 7/11/2013, 8/11/2013, 9/11/2013
点击按钮第二次我想要显示如下
10/11/2013, 11/11/2013, 12/11/2013, 13/11/2013, 14/11/2013, 15/11/2013, 16/11/2013
同样在每次点击按钮时,我想以这种格式显示剩下的日子。那么如何做到这一点,我已经尝试过这段代码,但它会显示
3/11/2013, 4/11/2013, 5/11/2013, 6/11/2013, 7/11/2013, 8/11/2013, 9/11/2013
我使用的代码
Calendar c = Calendar.getInstance();
// Set the calendar to monday of the current week
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
// Print dates of the current week starting on Monday
DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
for (int i = 0; i < 7; i++)
{
System.out.println(df.format(c.getTime()));
c.add(Calendar.DAY_OF_MONTH, 1);
}
答案 0 :(得分:1)
你几乎就在那里,只是根据输入参数
添加c.add(Calendar.WEEK_OF_YEAR, week);
来增加一周
public static void getDaysOfWeek(int week) {
Calendar c = Calendar.getInstance();
// Set the calendar to monday of the current week
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
c.add(Calendar.DATE, week * 7);
// Print dates of the current week starting on Monday
DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
for (int i = 0; i < 7; i++) {
System.out.println(df.format(c.getTime()));
c.add(Calendar.DAY_OF_MONTH, 1);
}
}
使用上述方法获取一周中的日期。只是第一次将0传递给方法,将1传递给下一次单击,依此类推。您将获得一周中的相应日期。
答案 1 :(得分:0)
public static void main(String[] args) {
int next = 1;
for(int i =0 ;i< 4 ;i++)
weekOfGivenMonth(next++);
}
private static void weekOfGivenMonth(int weekNext) {
Calendar c = Calendar.getInstance();
c.set(Calendar.WEEK_OF_MONTH, weekNext);
DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy");
for (int i = 0; i < 7; i++) {
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
}
试试这个我编辑的要求。我使用循环而不是你应该使用按钮来调用该月的第二,第三和第四周。