1
votes

Following is the code I used to calculate the number of weeks in a Month. But actually I need number of weeks with each week's start day as MONDAY and end day as SUNDAY. For example, JANUARY, 2012 will have 5 weeks. But with the above criteria, it will have 6 weeks.
January 2012
first week - Sunday 01
second week - 2 Mon to 8 Sunday
Third week - 9 Monday to 15 Sunday
fourth week - 16 Mon to 22 Sunday
fifth week - 23 Mon to 29 Sunday
Sixth week - 30 Monday to 31 Tuesday.

The following code gives only 5 weeks.

public class Test { 
    public static void main(String[] args) 
    { 
        Calendar calendar = Calendar.getInstance(); 
        int year = 2012; 
        int month = Calendar.JANUARY; 
        int date = 1; 
        calendar.set(year, month, date); 
        int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
        int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); 
        System.out.println("Number of Days In Month: " + numOfDaysInMonth); 
        System.out.println("Number of Weeks In Month: " + numOfWeeksInMonth);

}

}

The output for the above code is
Number of Days In Month: 31
Number of Weeks In Month: 5

But I need to get the "Number of Weeks In Month:" as 6
*Also I am trying to get the start date and the end date of each week in ddMMYYYY format.. * I am still working on it.

Can anyone please help me in fixing this?

4
If your application has to perform a lot of date and time calculations, I would recommend that you use the Joda Time library (Apache 2 license). With it, everything becomes much simpler.Eduardo
As for Joda-Time, that project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.Basil Bourque

4 Answers

2
votes

Add this to get "Number of Weeks In Month:" as 6,

calendar.setFirstDayOfWeek(Calendar.MONDAY);
1
votes

try this function

public static void main(String[] args) {
    System.out.println(getNumberOfWeeks(2012, Calendar.JANUARY));
}

static int getNumberOfWeeks(int year, int month) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, 1);
    int numOfWeeksInMonth = 1;
    while (c.get(Calendar.MONTH) == month) {
        c.add(Calendar.DAY_OF_MONTH, 1);
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            numOfWeeksInMonth++;
        }
    }
    return numOfWeeksInMonth;
}
1
votes

try this:

public static int noWeeks(int year,int month)
{
     Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, 1);


    int initDay = c.get(Calendar.DAY_OF_WEEK)-1
            ;
    int days =  c.getActualMaximum(Calendar.DAY_OF_MONTH);
    int a = (initDay==0?7:initDay)+days-1;
    return a/7+(a%7==0?0:1);

}
0
votes

O algoritmo correto é esse:

public static int getTotalWeeksOfMonth(int month, int year){

    int monthIndex = month-1;

     Calendar c = Calendar.getInstance();
     c.set(Calendar.YEAR, year);
     c.set(Calendar.MONTH, monthIndex);
     c.set(Calendar.DAY_OF_MONTH, 1);

     int numOfWeeksInMonth = 0;
     while (c.get(Calendar.MONTH) == monthIndex) {
         c.add(Calendar.DAY_OF_MONTH, 1);
         int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
         if ( dayOfWeek == Calendar.SUNDAY) {
             numOfWeeksInMonth++;
         }
     }

     if (c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
         numOfWeeksInMonth++;
     }

     return numOfWeeksInMonth;
}