Find which nth occurrence of a day per month for a date in Java

I need to find which nth dayOfWeek is a specific day in a month for a date in Java. For example, today is April 20, 2016, which is 3rd Wednesday of the month or October 31, 2016, which is 5th on Monday in October. How can I find which number indicates a specific period of the day in a month?

+1
source share
1 answer

Use the get method of the Calendar class.

public static int getOccurenceOfDayInMonth() {
    return Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH);
}

[EDIT]

Here is a solution giving any date, not the current date.

public static int getOccurenceOfDayInMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
}
+1
source

Source: https://habr.com/ru/post/1696351/


All Articles