41554

Question:
I need to find which nth dayOfWeek a particular day is in the month for a date in Java. For example, today is April 20th, 2016 which is the <strong>3rd</strong> Wednesday in the month or October 31, 2016 which is the <strong>5th</strong> Monday in October. How can I find which number the particular occurrence of a day is in the month?
Answer1: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 given any date, rather than 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);
}