9

I just wanna ask what is the efficient way to get the week numbers of a certain month. I have a function with month number & year as arguments, the return value of the function should be a int array which contain the week numbers of the specific month.(like following...)

public int[] getWeeksOfMonth(int month, int year){
            //what's the efficient way to implement this??
}
Mellon
  • 37,586
  • 78
  • 186
  • 264
  • The question is not clear and accept answers to your previous questions. – Emil Oct 25 '10 at 11:05
  • duplicate of one of your other question you didn't accepted ! http://stackoverflow.com/questions/3941700/how-to-get-dates-of-a-week-i-know-week-number – Alois Cochard Oct 25 '10 at 11:10

5 Answers5

9

The WEEK_OF_YEAR attribute of the Calendar class can be usefull for you.

Create a new date that will be the first day of the given month. Get the week of the year for this day, let say you got start value.

Create a new date that will be the last day of the given month. Get the week of the year for this day, so now you got end value.

Finally, create a simple int[] that will contains values from start to end.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • 2
    You dont have to iterate through the whole month. Just have a look at the first day's week, say `s`, and the last day's week, say `e`, and build the array holding `e-s+1` values from `s` to `e`. – Flinsch Oct 25 '10 at 11:18
  • @Flinsch Yes, you're right. I've updated my answer. – Romain Linsolas Oct 25 '10 at 11:44
  • 2
    Flinsch: This will not work in all cases. See for example January 2010. The three first days of the year belong to week 53. The fourth day belongs to week 1. – Grodriguez Oct 25 '10 at 12:17
  • You're right, sorry. So you need special case handling for `s>e` (which only may apply for january) as follows: If `s>e`: build array like `{ s, 1, ..., e }`. Else: build array like `{ s, ..., e }`. – Flinsch Oct 25 '10 at 12:36
5

Calendar cal = Calendar.getInstance();

int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);

rajeesh
  • 937
  • 10
  • 11
  • This gives you how many weeks there are in a certain month, which usually varies from 5 to 6 (with the exception of february, which can be only 4 weeks) – Maragues Mar 12 '13 at 10:53
  • But this does not work in one scenario where in is user change the Date format. I have a open question on this only. https://stackoverflow.com/questions/61537895/need-to-get-correct-output-for-number-of-weeks-in-a-month-irrespective-of-what-d Any answer on this is appreciated. – Dhaval Shah May 01 '20 at 07:24
3

I think, that this is much simpler way:

Calendar now = Calendar.getInstance();

System.out.println("Current week of month is : " +
            now.get(Calendar.WEEK_OF_MONTH));
CookieMonssster
  • 658
  • 1
  • 6
  • 19
3

I am not sure whether you want to return an array of length equal to the number of days in the month, with each value being the week number for the corresponding day, or an array of all distinct week numbers for the days in the specified month. Assuming it is the former, this should work:

public static int[] getWeeksOfMonth(int month, int year)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    int weeks[] = new int[ndays];
    for (int i = 0; i < ndays; i++)
    {
        weeks[i] = cal.get(Calendar.WEEK_OF_YEAR);
        cal.add(Calendar.DATE, 1);
    }
    return weeks;
}

If you want an array of distinct week numbers for the days in the specified month:

public static Integer[] getWeeksOfMonth(int month, int year)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    Set<Integer> weeks = new HashSet<Integer>();
    int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    for (int i = 0; i < ndays; i++)
    {
        weeks.add(cal.get(Calendar.WEEK_OF_YEAR));
        cal.add(Calendar.DATE, 1);
    }

    return weeks.toArray(new Integer[0]);
}

(Note this last example returns an array of Integer objects, but it is trivial to modify it to return an array of int instead)

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
0
  1. Create Calendar instances for the first and last day of the month.
  2. then on each instance invoke the get(Calendar.WEEK_OF_YEAR)
  3. Step [2] will return starting and ending index for the weeks
anirvan
  • 4,797
  • 4
  • 32
  • 42