Get first next Monday after certain date?

前端 未结 5 664
自闭症患者
自闭症患者 2020-12-01 20:56

I know there is the same question here, but I have tried the answer provided and it returned an output that I don\'t understand. I am confused by the answer and I don\'t thi

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 21:35

    Just a suggestion - Instead of looping, you could just use the switch statement along with the Calendar.add() like this:

    int weekdayNum = c.get(Calendar.DAY_OF_WEEK);
    
        switch (weekdayNum) {
        case 1: {
            c.add(Calendar.DAY_OF_MONTH, 1);
            break;
        }
        case 3: {
            c.add(Calendar.DAY_OF_MONTH, 6);
            break;
        }
        case 4: {
            c.add(Calendar.DAY_OF_MONTH, 5);
            break;
        }
        case 5: {
            c.add(Calendar.DAY_OF_MONTH, 4);
            break;
        }
        case 6: {
            c.add(Calendar.DAY_OF_MONTH, 3);
            break;
        }
        case 7: {
            c.add(Calendar.DAY_OF_MONTH, 2);
            break;
        }
        default:
            break;
        }
    

提交回复
热议问题