tags:

views:

444

answers:

2

Exact duplicate: http://stackoverflow.com/questions/590385/how-to-get-all-dates-of-sundays-in-a-particular-year-in-java


int year = 2009;
Calendar cal = new GregorianCalendar(year, Calendar.JANUARY, 1);
for (int i = 0, inc = 1; i < 366 && cal.get(Calendar.YEAR) == year; i+=inc) {
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        // this is a sunday
        cal.add(Calendar.DAY_OF_MONTH, 7); 
        inc = 7;
    } else {
        cal.add(Calendar.DAY_OF_MONTH, 1);
    }
}

In Miles D given code, I am getting dates of all sundays from second sunday of frst month to frst sunday of next year. Actually I am looking for a particular year only, and its all sunday dates, plz miles can u see it once again.....

For instance, I need all sunday dates in 2009 from January 2009 to December 2009 only

+1  A: 

Here's the algorithm I might start with (code is left as an exercise for the reader):

Find a Sunday closest to a specific date. For example, start from Jan 1 and walk forward until you find a Sunday. This should cost no more than 7 operations.

Now that you know a Sunday, you can calculate all of the other ones from Julian dates based on that one simply by adding multiples of 7 to the first Sunday.

You should also calculate the closest Sunday backward from Dec 31, so that you know when to stop including Sundays in your chosen year.

Oh, having just written that, I see that someone else had the same idea already.

Craig S
thanks for your kind suggestion my dear Craig S,but exactly am looking for code to get all date of sundays in a selected year.
+1  A: 

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?

  1. The loop starts with an i of 0 and an inc of 1
  2. 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.
  3. 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 added 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?

scraimer
+1 for the effort =D Btw, I don't think the OP said anything about counting the Sundays.
Zach Scrivena
Darnit, you're absolutly right!
scraimer
Hai Scraimer...thanks for ur explanation,but am bit aware of programing,all i expecting from u is.need the exact code for my requuiremnt,actully this code is given by one dude,but it's not giving output,i need all dates of sundays(52/53 sunday dates) in a year,thats it,can u Do it for me
@scraimer: Guess that confirms your suspicions ;)
Zach Scrivena
Nope. This site isn't about giving code, it's about giving programmers advice on how to figure out how to do things themselves... I was hoping you would have understood that from my answer.
scraimer
ok Scraimer,aftr all am a Student how can i get that think quickly..so finally u r words saying indirectly "StackOverFlow cant helps 100%"