Reputation: 18068
I wanted to roll 2 days forward if it is mon, tue or wed and to the next monday if it is thu or fri. But I have problem with logic formula. It looks like Java programmers have not set final values right. The first statement seems to be always true regardless of day of the week.
import static java.util.Calendar.*;
import java.util.GregorianCalendar;
public class Demo {
public static void main(String[] args) {
GregorianCalendar gc = new GregorianCalendar();
gc.roll(DAY_OF_WEEK, 2);
if((gc.get(DAY_OF_WEEK) & (MONDAY | TUESDAY | WEDNESDAY)) == gc.get(DAY_OF_WEEK)){
System.out.println("it is monday or tuesday or wednesday");
gc.roll(DAY_OF_WEEK, 2);
}else{
System.out.println("other day");
}
}
}
Upvotes: 0
Views: 94
Reputation: 124275
Days of week represents values
SUNDAY = 1;
MONDAY = 2;
TUESDAY = 3;
WEDNESDAY = 4;
THURSDAY = 5;
FRIDAY = 6;
SATURDAY = 7;
so all you need to do is check if result of gc.get(DAY_OF_WEEK)
is between 2
and 4
(including), so maybe change your condition to
if (MONDAY <= gc.get(DAY_OF_WEEK) && gc.get(DAY_OF_WEEK) <= WEDNESDAY) {
Remember, premature optimization is root of all evil. Focus on writing code for humans.
Upvotes: 4