I want to get number of weeks in a particular month

后端 未结 5 1081
深忆病人
深忆病人 2021-01-01 16:01

i want to get number of weeks in current month.That including the previous month\'s days in the last week and the first week days in the next month.

Something like t

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 16:44

    Let's assume we know the first day of the month (Saturday, Sunday etc).

    • If 1st day of the month is a Sunday and it's not a leap-year, then the (correction edit) February month will have only 4 weeks. Otherwise, in all the cases, it should have at least 5 and at most 6 weeks.
    • Now, if your month starts on Saturday, you'll always have 6 weeks (excluding if it is February, when it there will be 5)
    • Now, if your month starts on Friday, you'll have 6 weeks for months having 31 days and 5 for all others.
    • For rest of the cases, it will always be 5 weeks.

    Finding out the 1st day of the month shouldn't be difficult, you can use Calendar or other date/time utility library classes. However, I guess there could be cleaner solutions removing the corner cases and unnecessary complexity.

提交回复
热议问题