Java Calendar using Calendar.DAY_OF_WEEK to get the first and the last dates for a particular date

前端 未结 3 2024
不知归路
不知归路 2021-01-28 19:49

In my application there lies a code which works abruptly sometimes, its about getting a week interval using the java calendar object through Calendar.DAY_OF_WEEK. The code check

3条回答
  •  醉酒成梦
    2021-01-28 19:56

    Be very clear on your desired behaviour here. You start with a calendar object whose "now" is some day of the week, perhaps "today". You the call set(DAY_OF_WEEK, ...). What effect do you desire if the Calendar's today is Tuesday? Sunday? Monday?

    As observed in other answers, what happens depends upon the Calendar's opinion about what the First day of week is. So first set that to your chosen value. You will then (according to this answer get a Sunday and a Monday in the current week, which may not be what you want - what exactly do you need if today is Sunday? - some systems might actually be "thinking" about the next week.

    Personally I might get my Monday according to some business rules and the get the Sunday after by adding 6 days.

提交回复
热议问题