获取 2 个日期之间的周数,其中周数跨越 2 个月
我想得到两个日期之间的所有周,并且每个月计算两次跨越两个月的周。例如,2021 年的第 14 周同时举办了 3 月和 4 月,因此在这种情况下,我希望该周计算两次(一次为 3 月,一次为 4 月)。我查看并发现只有一些库可以计算两个日期之间的周数。我想我可以得到周数和月份数并形成一个独特的数组,但这似乎有点过头了。有人有什么建议吗?
I would like to get all the weeks between 2 dates with weeks that cross 2 months counted twice for each month. For example, in 2021 week 14 of the year hosted both March and April so in this case, I would like that week counted twice (once for March and once for April). I've looked and found just libraries that count the number of weeks between 2 dates. I think I could get week numbers and month numbers and form a unique array but this seems a bit over the top. Has anyone got any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的代码允许仅使用标准
LocalDate
类及其方法isBefore()
、plusWeeks(),<代码>plusDays()。
请记住,星期几和月份由
java.time
包中的枚举表示。我做了几个假设:
输出
The code below allows to do that by utilizing only the standard
LocalDate
class and it's methodsisBefore()
,plusWeeks()
,plusDays()
.Keep in mind the days of the week and months are represented by enums from the
java.time
package.I've made a couple of assumptions:
Output
好吧,这可以通过 Java 日期和时间 API (
java.time
) 和 Java Streams API (java.util.stream
) 的组合来实现:这里会发生什么,如下。
首先,日期被规范化,即,它们被设置为一周的开始(根据 ISO 标准为星期一)。
然后我们遍历每周的星期一,并检查该周的最后一天(星期日)是否在同一个月内。如果是,则生成
1
,否则生成2
。最后,我们将所有产生的值相加。
请注意,我假设一周从星期一开始(ISO)。该代码还将开始日期和结束日期所在的星期视为完整日期。
Well, this is achievable with a combination of the Java Date and Time API (
java.time
) and the Java Streams API (java.util.stream
):What happens here, is as follows.
First, the dates are normalized, that is, they are set to the start of the week (Monday according to ISO standards).
Then we walk over the Monday of each week, and check if its last day of the week (Sunday) lies within the same month. If it is, then it yields
1
, otherwise it yields2
.At last, we sum all yielded values.
Note that I assumed that a week starts on Monday (ISO). The code also considers the week of both the start date as the end date as full ones.
您可以使用 java.time 获取这样的周数:
您没有提及您正在使用哪个 java 版本。 java.time是在java 8中引入的。对于java 8之前的版本还有其他可用的解决方案。
基于上述,您应该能够解决您的问题。
You can get the weeknumber like this using java.time:
You did not mention which java version you are using. java.time was introduced in java 8. There are other solutions available for pre-java 8.
Based on the above, you should be able to solve your problem.