Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

1. ## GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

HI,
I could calculate the number of weeks in any month and year using following method.

public void calWeeksAndDays(int month, in year)
{
GregorianCalendar cal = new GregorianCalendar(year, month, 1);

int weeks = cal.getActualMaximum(WEEK_OF_MONTH);

}

This works as expected, but now how do I figure out how many days are in each of the weeks that I got from above?

2. ## Re: GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

Aren't there 7 days in each week?

3. ## Re: GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

Not necessarily. For example: March 2013 has actually 4 full weeks and two partial ones. The first week of March has only 2 days, i.e. Thursday March 1 and Friday March 2. While the last week of March has only 1 day, that is Sunday Mar 31.

So that's what I want to figure out.

4. ## Re: GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

Ohhhh.

So isn't the number of days in the partial week = number of days in the month less the number of full weeks * 7?

5. ## Re: GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

Hello.
Check the name of the first day of the month. Based on that you will know whether its a partial week or full week. If its partial week you will know how many days. If its full then its 7 days. You will also know how many maximum days are there in that month.
After finishing with first week start increments of seven until you cross the maximum days. If you have crossed the maximum days then decrement by 7 and count the remaining days. Those will be the number of days for the last week.

Syed.

6. ## Re: GregorianCalendar - How to figure out how many days in all weeks of March, 2013?

Originally Posted by syedbhai
Hello.
Check the name of the first day of the month. Based on that you will know whether its a partial week or full week. If its partial week you will know how many days. If its full then its 7 days. You will also know how many maximum days are there in that month.
After finishing with first week start increments of seven until you cross the maximum days. If you have crossed the maximum days then decrement by 7 and count the remaining days. Those will be the number of days for the last week.

Syed.
Thankyou so much for everyone's feedback. This did give me ideas how to solve the problem. Thanks rsinha