-2

How do you find the total number of Mondays (e.g. 4 or 5) in a particular month ???

Calendar c = Calendar.getInstance();
int mon = c.getActualMaximum(Calendar.MONDAY);

is this right way ??

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Devendra
  • 125
  • 2
  • 10

4 Answers4

1

you can use this method

public int countMonday(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    // Note that month is 0-based in calendar, bizarrely.
    calendar.set(year, month - 1, 1);
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int count = 0;
    for (int day = 1; day <= daysInMonth; day++) {
        calendar.set(year, month - 1, day);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.MONDAY) {
            count++;
            // Or do whatever you need to with the result.
        }
    }
    return count;
}

Updated

public int countDayOccurence(int year, int month,int dayToFindCount) {
    Calendar calendar = Calendar.getInstance();
    // Note that month is 0-based in calendar, bizarrely.
    calendar.set(year, month - 1, 1);
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int count = 0;
    for (int day = 1; day <= daysInMonth; day++) {
        calendar.set(year, month - 1, day);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == dayToFindCount) {
            count++;
            // Or do whatever you need to with the result.
        }
    }
    return count;
}

And then you can call this method for each day name

   int countMonday = countDayOccurence(year,month,Calendar.MONDAY);
   int countTuesday = countDayOccurence(year,month,Calendar.TUESDAY);

...............................................

Ramzan Zafar
  • 1,562
  • 2
  • 17
  • 18
0

Using the Calendar api, the best option I can see is to:

  1. get the actual maximum day of month (i.e. how many days in this month)

  2. set calendar to first day of the month and get the day of the week

  3. calculate how many mondays could occur (i.e. if 28 days in month - 4, if 29 4 unless month started on a monday, if 30, 4 unless month started on monday or tuesday, if 31, 4 unless started on monday, tuesday, or wednesday).

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
0

Here is what you need at least for the current month :

    Calendar c = Calendar.getInstance();        
    int maxDaysInMonth = c.getMaximum(Calendar.DAY_OF_MONTH);
    int firstMonday = c.get(Calendar.MONDAY); // first monday in the month (Beware, 0 is the first day of the month)
    int mondays = 0;
    int i=firstMonday;
    while(i<maxDaysInMonth){
        mondays++;
        i+=7;
    };
    System.out.println(mondays);
singe3
  • 2,065
  • 4
  • 30
  • 48
  • I'm asking you what error do you get. Describe it please. – singe3 Jul 18 '14 at 12:47
  • Listening on javadebug Not able to submit breakpoint LineBreakpoint Customer_Bills.java : 11, reason: The breakpoint is set outside of any class. Invalid LineBreakpoint Customer_Bills.java : 11 User program running User program finished – Devendra Jul 18 '14 at 12:48
  • Not so hard you see ? You must have a breakpoint somewhere. Just remove it. It has nothing to do with the while loop which has nothing special – singe3 Jul 18 '14 at 12:50
  • But if i want for others days also in same code then how to code ? – Devendra Jul 18 '14 at 18:11
0

As per my question.

@Ramzan Zafar: Answer is right and more in that i created my calender instance for current year and month for year and month. I got my answer as per my question.

Devendra
  • 125
  • 2
  • 10