New to programming? Well, let's have a look at the code together!
The first thing that I see is the conditional inside the loop:
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
This checks if the current date stored in the Calendar cal
has a DAY_OF_WEEK
which is SUNDAY
. If it is, it does one thing, and if it isn't, it does another.
Now that we know what the loop's "brain" is looking for, let's look at the loop's declaration:
for (int i = 0, inc = 1; i < 366 && cal.get(Calendar.YEAR) == year; i+=inc)
The for
loop is split into three parts: initialization, condition, and "increment".
The initialization, is run only once, before th loop starts running:
int i = 0, inc = 1
Which, as I'm sure you know, creates two variables: i
and inc
. It sets i
to 0, and inc
to 1.
The condition of the loop gets checked before running each iteration of the loop. It is:
i < 366 && cal.get(Calendar.YEAR) == year
Which checks whether i
is still less than 366 and also that the date stored in cal
has the YEAR
of year
. This, in effect, makes sure we are still in the same year we started with.
The last section of the for, is this:
i+=inc
Which adds the value of inc
to the value of i
and stores it in i
.
So what do we have so far in the loop?
- The loop starts with an
i
of 0 and an inc
of 1
- The loop keeps going as long as
i
is smaller than the length of a Gregorian year (366 on leap-years),
- (AND) the date in
cal
still has the same year as the year in year
.
i
is advanced by inc
Let's have another look at the body of the loop:
if (condition_that_checks_if_it_is_a_sunday) {
// this is a sunday
cal.add(Calendar.DAY_OF_MONTH, 7);
inc = 7;
} else {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
As you can see, if it's a Sunday, two things happen: inc
is set to 7, and 7 DAY_OF_MONTH
are add
ed to cal
. This would mean that in the next iteration of the loop, the expression i+=inc
will mean i+=7
and not i+=1
which is did when inc
was 1 (like it was when the initialization was done.)
If it's not a Sunday, a single DAY_OF_WEEK
is added to cal
.
So what do we have in the body of the loop?
- Check if
cal
is a Sunday
- If it is, advance
cal
by 7 days. And not only that, but keep advancing i
by 7!
- If
cal
isn't on a sunday, advance it to the next day of the week, and let i
keep advancing by 1.
In other words, the loop will start at cal
's initial value, and move cal
forward one day at a time until it finds a Sunday. Once it does that, it will move cal
forward 7 days at a time, until it has gone over 366 days, or until cal
is in another year!
Thus, from (ahem) briefly examining the code above, we an conclude it doesn't actually "get" the Sundays in a year. It simply iterates over them, and it's up to you to add the code that actually does something with the Sundays it finds.
And now that we've gone over the code together, I'm certain it's trivial to do that! It really is a matter of just adding a couple of lines. You just have to add a line that does [insert here whatever you want] every time the loop enters the part where it "knows" cal
is a Sunday.
Simple, no?