Hello at first i would like to note that i have found several posts with the same question here, but NONE of them worked for me. i am creating alarm clock application for android and the last thing i need is: get the date of the nearest certain day in week.
I have found several algorithms here and i will also copy one here :
import java.util.Calendar;
public class NextWednesday {
public static Calendar nextDayOfWeek(int dow) {
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
if (!(diff > 0)) {
diff += 7;
}
date.add(Calendar.DAY_OF_MONTH, diff);
return date;
}
public static void main(String[] args) {
System.out.printf(
"%ta, %<tb %<te, %<tY",
nextDayOfWeek(Calendar.WEDNESDAY) // this can contain any of the 7 days in week
);
}
}
Today is tuesday in my country
If i put wednesday in the function it returns me the wednesday that is in the next week, but thats not correct.
This algorithm automatically looks at the following week no matter if its just monday and theres whole week before you, it jumps to the next week and does its job but thats not correct, i need to implement the same behaviour but it must start from today.
Example: Today is Monday, i am looking for wednesday
- Correct output: Date of wednesday in this week.
- Uncorrect output: Date of wednesday in the next week.
I hope its clear enough.