I want to get number of weeks in a particular month

The WEEK_OF_YEAR attribute of the Calendar class can be useful for you. Ref: Calendar class

Create a new date that will be the first day of the some 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.

Now end - start + 1 should be one that you want.

You may have to handle some corner case when the week overlaps to another year or similar. But I think once you get this right, you can do that with simple logic.

Here is simple example code. I think you can make it into function and pass whatever you want.

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2013);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int start = cal.get(Calendar.WEEK_OF_YEAR);
        Log.d("BLA BLA", "Value is " + start);
        cal.set(Calendar.YEAR, 2013);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 28);
        int end = cal.get(Calendar.WEEK_OF_YEAR);
        //Above line will not work for December Month, use following line for this
        int end = isDecember?53:cal.get(Calendar.WEEK_OF_YEAR);
        Log.d("BLA BLA", "Value is " + end);
        Log.d("BLA BLA", "Num weeks:: " + (end - start +1 ));

For Corner case:

Corner case will only occur for the month of Januaray (E.g Jan 2010, Jan 2000) in those cases most of the days are part of the last week of the previous year so the start value will be 52 and end value will be 5. When this occurs check if,

          if(start > end) {
                numweeks = end + 1;
             }

P.S: Put it to different testing inputs that you got. Let me know If I can improve on it once you find any fault.

And Even much simpler solution:

        Calendar cal = Calendar.getInstance();
        for(int i = 0 ; i < 11;i++){
            cal.set(Calendar.YEAR, 2013);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.MONTH, i);
            int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
            Log.d("LOG","max week number" + maxWeeknumber);
        }

        01-22 01:49:03.591: D/LOG(573): max week number5
        01-22 01:49:03.602: D/LOG(573): max week number5
        01-22 01:49:03.602: D/LOG(573): max week number6
        01-22 01:49:03.671: D/LOG(573): max week number5
        01-22 01:49:03.671: D/LOG(573): max week number5
        01-22 01:49:03.671: D/LOG(573): max week number6
        01-22 01:49:03.681: D/LOG(573): max week number5
        01-22 01:49:03.691: D/LOG(573): max week number5
        01-22 01:49:03.691: D/LOG(573): max week number5
        01-22 01:49:03.711: D/LOG(573): max week number5
        01-22 01:49:03.711: D/LOG(573): max week number5

Simple solution works fine with corner cases:

        Calendar cal = Calendar.getInstance();
        for(int i = 0 ; i < 11;i++){
            cal.set(Calendar.YEAR, 2010);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.MONTH, i);
            int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
            // Month value starts from 0 to 11 for Jan to Dec
            Log.d("LOG","For Month :: "+ i + " Num Week :: " + maxWeeknumber);
        }

Log:

        01-22 01:59:35.841: D/LOG(629): For Month :: 0 Num Week :: 6
        01-22 01:59:35.841: D/LOG(629): For Month :: 1 Num Week :: 5
        01-22 01:59:35.841: D/LOG(629): For Month :: 2 Num Week :: 5
        01-22 01:59:35.841: D/LOG(629): For Month :: 3 Num Week :: 5
        01-22 01:59:35.841: D/LOG(629): For Month :: 4 Num Week :: 6
        01-22 01:59:35.852: D/LOG(629): For Month :: 5 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 6 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 7 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 8 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 9 Num Week :: 6
        01-22 01:59:35.871: D/LOG(629): For Month :: 10 Num Week :: 5

use WEEK_OF_MONTH will be better, no cross year issue.

public static void main(String[] args) throws Exception {
  for (String m : new String[] { "2012-11", "2012-12", "2013-01", "2013-02", "2013-03",
              "2013-04", "2013-05", "2013-06" }) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
      Date date = format.parse(m);
      Calendar c = Calendar.getInstance();
      c.setTime(date);

      int start = c.get(Calendar.WEEK_OF_MONTH);

      c.add(Calendar.MONTH, 1);
      c.add(Calendar.DATE, -1);
      int end = c.get(Calendar.WEEK_OF_MONTH);
      System.out.println(" # of weeks in " + format.format(c.getTime())
              + ": " + (end - start + 1));
  }
}

results:

# of weeks in 2012-11: 5
# of weeks in 2012-12: 6
# of weeks in 2013-01: 5
# of weeks in 2013-02: 5
# of weeks in 2013-03: 6
# of weeks in 2013-04: 5
# of weeks in 2013-05: 5
# of weeks in 2013-06: 6

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work (I was frankly negatively surprised to see that no answer so far had suggested this).

public static int getTotalNumberOfWeeks(WeekFields wf, Temporal month) {
    YearMonth ym = YearMonth.from(month);
    int firstWeekNumber = ym.atDay(1).get(wf.weekOfMonth());
    int lastWeekNumber = ym.atEndOfMonth().get(wf.weekOfMonth());
    return lastWeekNumber - firstWeekNumber + 1; // Both weeks inclusive
}

Let’s first try it with the example month from your question, January 2013, so we know that the expected result is 5. Since your weeks start on Sunday, we specify that too in the call:

    System.out.println(getTotalNumberOfWeeks(
            WeekFields.SUNDAY_START, YearMonth.of(2013, Month.JANUARY)));

Output is the expected:

5

For a newer example I am taking May 2021 (next month as of writing). Since where I live we use ISO weeks (starting on Monday), let’s try that too:

    System.out.println(getTotalNumberOfWeeks(
            WeekFields.ISO, YearMonth.of(2021, Month.MAY)));

6

If you want the weeks used in the default locale of your JVM, use WeekFields.of(Locale.getDefault()) as first argument. If you want the current month, you may pass YearMonth.now(ZoneId.systemDefault()) as the second. I declared the second paramtere a Temporal for convenience. This allows you to pass a LocalDate, a YearMonth, a ZonedDateTime, any java.time class that defines a month and a year.

ISO 8601, the international standard probably used in the greater part of the world, defines week 1 of the month as the first week that contains at least 4 days of the month. This implies that up to three days in the beginning of the month may be in week 0. This is why the answers that only look at the maximum week number of the month break. Instead I subtract the minimum week number from the max and add 1 because we want both of those weeks included in the count.

Link

Oracle tutorial: Date Time explaining how to use java.time.


We can use the calendar class and then do something like

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));

this will return the number of weeks in the current month