So, I have an application where user can select a date .
The maximum date that he can select is upto 7 more days from the current date.
Now, I display a calendar for the current month only. If the current date is 30th July then the user must be provided an option to select date upto 6th Aug also I have to handle the case where the year changes i.e. 30th Dec,15 is the current date and +7 days provide 6th Jan,16.
Below is the code that I am using to get all the days of current month. What should I do to get it work for the above scenario.
Note: I always have the current date available.
Code:
public class Cals {
public static void main(String args[])
{
Calendar start = Calendar.getInstance();
start.set(Calendar.DAY_OF_MONTH, Calendar.getInstance()
.getActualMinimum(Calendar.DAY_OF_MONTH));
Calendar end = Calendar.getInstance();
end.set(Calendar.DAY_OF_MONTH,
Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
Calendar now = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM");
for (Date dt = start.getTime(); !start.after(end); start.add(
Calendar.DATE, 1), dt = start.getTime()) {
System.out.println(sdf.format(dt));
}
}
}
Current O/p:
Wed 1 Jul
Thu 2 Jul
Fri 3 Jul
Sat 4 Jul
Sun 5 Jul
Mon 6 Jul
Tue 7 Jul
Wed 8 Jul
Thu 9 Jul
Fri 10 Jul
Sat 11 Jul
Sun 12 Jul
Mon 13 Jul
Tue 14 Jul
Wed 15 Jul
Thu 16 Jul
Fri 17 Jul
Sat 18 Jul
Sun 19 Jul
Mon 20 Jul
Tue 21 Jul
Wed 22 Jul
Thu 23 Jul
Fri 24 Jul
Sat 25 Jul
Sun 26 Jul
Mon 27 Jul
Tue 28 Jul
Wed 29 Jul
Thu 30 Jul
Fri 31 Jul
Expected O/p:
Wed 1 Jul
Thu 2 Jul
Fri 3 Jul
Sat 4 Jul
Sun 5 Jul
Mon 6 Jul
Tue 7 Jul
Wed 8 Jul
Thu 9 Jul
Fri 10 Jul
Sat 11 Jul
Sun 12 Jul
Mon 13 Jul
Tue 14 Jul
Wed 15 Jul
Thu 16 Jul
Fri 17 Jul
Sat 18 Jul
Sun 19 Jul
Mon 20 Jul
Tue 21 Jul
Wed 22 Jul
Thu 23 Jul
Fri 24 Jul
Sat 25 Jul
Sun 26 Jul
Mon 27 Jul
Tue 28 Jul
Wed 29 Jul
Thu 30 Jul
Fri 31 Jul
Sat 1 Aug
Sun 2 Aug
Mon 3 Aug
Tue 4 Aug
Wed 5 Aug
Thu 6 Aug