I am trying to add one month to the date depending on weekdays. For example, the date is 3. Monday of September. After adding, I want to have 3. Monday of October. I tried to add a month before the next date.
Mon Sep 17 17:30:00 MESZ 2012
using this code:
Calendar date = Calendar.getInstance(); date.setTimeInMillis(date_in_mil); date.add(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
But I got
Mon Oct 08 17:30:00 MESZ 2012
which is the second Monday of October, not the third. Does anyone know how this works?
EDIT This is the solution I used, as in the answer below:
int prevDayOfWeek = date.get(Calendar.DAY_OF_WEEK); date.add(Calendar.MONTH, 1); date.set(Calendar.DAY_OF_WEEK, prevDayOfWeek); date.set(Calendar.DAY_OF_WEEK_IN_MONTH, week);
there was a week - this is the number of the week per month. Example 1 - first, second, etc. But a week can also take into account the other side, for example, example -1 means the last week of the month.
source share