How to find the week of the month

I like to know what week of the month a particular day falls. For example, September 20, 2012 falls on the 4th week of September, but the code below displays it as 3, which is incorrect. The system divides the days by 7 and returns the output, but I do not need this. I searched in the Joda API but could not find a solution. Please let me know if there is a way to find out the week of the month, the day is falling

    Calendar ca1 = Calendar.getInstance();

    ca1.set(2012,9,20);

    int wk=ca1.get(Calendar.WEEK_OF_MONTH);
    System.out.println("Week of Month :"+wk);
+5
source share
5 answers

There are two reasons for this:

The first one (from the API ):

The first week of a month or year is defined as the earliest seven-day period starting with getFirstDayOfWeek () and containing at least getMinimalDaysInFirstWeek () days

( 4),

Calendar.setMinimalDaysInFirstWeek()

- , @Timmy . , . :

public static void main(String[] args) {
    Calendar ca1 = Calendar.getInstance();
    ca1.set(2012, Calendar.SEPTEMBER, 20);
    ca1.setMinimalDaysInFirstWeek(1);
    int wk = ca1.get(Calendar.WEEK_OF_MONTH);
    System.out.println("Week of Month :" + wk);
}

Week of Month :4
+7

. ca1.set(2012,9,20) .

+3

To verify that the correct month is set, try using the month constants provided by Calendar-Class.

+1
source

For those who work with Joda time, this is what I use:

/**
 * For a week starting from Sunday, get the week of the month assuming a
 * valid week is any week containing at least one day.
 * 
 * 0=Last,1=First,2=Second...5=Fifth
 */
private int getWeekOfMonth( MutableDateTime calendar )
{
    long time = calendar.getMillis();
    int dayOfMonth = calendar.getDayOfMonth();
    int firstWeekday;
    int lastDateOfMonth;
    int week;
    int weeksInMonth;

    calendar.setDayOfMonth( 1 );
    firstWeekday = calendar.getDayOfWeek();
    if (firstWeekday == 7)
    {
        firstWeekday = 0;
    }
    calendar.setHourOfDay( 0 );
    calendar.addMonths( 1 );
    calendar.addDays( -1 );
    lastDateOfMonth = calendar.getDayOfMonth();
    weeksInMonth = (int)Math.ceil( 1.0*(lastDateOfMonth + firstWeekday)/7 );
    week = (byte)(1 + (dayOfMonth + firstWeekday - 1)/7);
    week = (week == weeksInMonth)?0:week;
    calendar.setMillis( time );

    return week;
}
0
source
class test {
    public static void main(String[] args){
        Calendar cal = Calendar.getInstance();      
        cal.set(2016, Calendar.AUGUST, 01);
        printDayOFWeek(cal, Calendar.MONDAY, 5);//prints monday on 5th week
    }
}

private static void printDayOFWeek(Calendar cal, int day, int whatweek) {
    cal.set(Calendar.DAY_OF_WEEK, day);//day = Mon, tue, wed,..etc
    cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1); //-1 return last week 

    Date last = cal.getTime();
    cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, whatweek);

    Date one = cal.getTime();
    if(one.before(last) || one.compareTo(last) ==0)
    {
      System.out.println(whatweek +"WEEK" + cal.getTime());
    }
}
-1
source

All Articles