Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get third friday of a month

Tags:

java

calendar

I am developing a local calendar for my application. but the is an issue with monthly repeat event (day of week).

When i am create an event starting on 16-9-2016(16 SEP 2016 FRIDAY) and repeating Third Friday of each month. but next month it create on second Friday 14-10-2016 (This is the issue). next month it will be on third Friday.

my code is

public Date nthWeekdayOfMonth(int dayOfWeek, int month, int year, int week, TimeZone timeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(timeZone);
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

I know the issue. but i don`t know how to fix it.. is there any way to fix it ?

like image 448
Ajmal Muhammad Avatar asked Sep 16 '16 09:09

Ajmal Muhammad


People also ask

How do you get the first Monday of the month in Python?

To find the first Monday of a given month, we are going to use the numpy module i.e the numpy. busday_offset() method in numpy module. Parameters: date: The array of dates to process.


2 Answers

You code seems to be working completely fine, there is nothing that is going wrong from what I can see, it may be that your parameters are wrong.

It is important to note that MONTH and DAY are 0-based so, 0 = January and 0 = Sunday so your parameters for getting the third friday should look like the following:

nthWeekdayOfMonth(6, 9, 2016, 3, TimeZone.getTimeZone("Europe/London"));

Which returns the following output:

Fri Oct 21 11:06:33 BST 2016

To break it down:

  1. Day of week is 6, because Sunday = 0.
  2. Month is 9 - i.e. October
  3. Year is normal - 2016
  4. Week is NOT 0-based so 3rd week will be index 3
  5. TimeZone as normal

Please see the Calendar documentation for reference.


EDIT

So for some reason, it works on my machine but it doesn't on others; I don't know what the issue could be with that but using DAY_OF_WEEK_IN_MONTH seems to be a better option for this:

public static Date nthWeekdayOfMonth(int dayOfWeek, int month, int year, int week, TimeZone timeZone) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(timeZone);
    calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    //calendar.set(Calendar.WEEK_OF_MONTH, week);
    calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, week);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    return calendar.getTime();
}

I usually use GregorianCalendar but Calendar should work just fine.

This should (hopefully) work for the most part, I've tested it on other machines and ideone.

like image 159
px06 Avatar answered Sep 18 '22 02:09

px06


I could propose next decision:

public Date nthWeekdayOfMonth(int dayOfWeek, int month, int year, int week, TimeZone timeZone) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(timeZone);
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    // add +1 to week if first weekday of mounth > dayOfWeek
    int localWeek = week;
    if (calendar.get(calendar.DAY_OF_WEEK) > dayOfWeek) {
        localWeek++;
    }
    calendar.set(Calendar.WEEK_OF_MONTH, localWeek);
    calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    return calendar.getTime();
}

for:

System.out.println(nthWeekdayOfMonth(Calendar.FRIDAY, Calendar.SEPTEMBER, 2016, 3, TimeZone.getTimeZone("Europe/London")));
System.out.println(nthWeekdayOfMonth(Calendar.FRIDAY, Calendar.OCTOBER, 2016, 3, TimeZone.getTimeZone("Europe/London")));
System.out.println(nthWeekdayOfMonth(Calendar.FRIDAY, Calendar.NOVEMBER, 2016, 3, TimeZone.getTimeZone("Europe/London")));

it returns:

Fri Sep 16 19:41:23 YEKT 2016
Fri Oct 21 19:41:23 YEKT 2016
Fri Nov 18 20:41:23 YEKT 2016
like image 32
mv200580 Avatar answered Sep 17 '22 02:09

mv200580