检查 joda.time.Interval 是否正好跨越 1 个日历周的更好方法(考虑夏令时等)
有更好的方法吗?
boolean oneCalendarWeek = interval.getStart().plusWeeks(1).equals( interval.getEnd() );
我猜由于 equals 的实现方式,以下内容将不起作用......
boolean oneCalendarWeek = interval.toPeriod().equals( Weeks.ONE );
Is there a better way of doing this?
boolean oneCalendarWeek = interval.getStart().plusWeeks(1).equals( interval.getEnd() );
I guess the following won't work because of the way equals is implemented...
boolean oneCalendarWeek = interval.toPeriod().equals( Weeks.ONE );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自评论:
虽然使用
Weeks.ONE
的示例不起作用(因为Period.equals()< /code> 首先检查两个
Period
实例是否支持相同数量的字段,而Weeks.ONE
仅支持一个字段),这应该有效:这里是一个代码示例,用于测试从 DST 开始之前开始到 DST 期间结束的时间间隔。但是,我不能 100% 确定如果间隔的开始或结束时间恰好落在 DST 边界上,这会如何表现。
From the comments:
While the example using
Weeks.ONE
does not work (sincePeriod.equals()
first checks if the twoPeriod
instances support the same number of fields, andWeeks.ONE
only supports one field), this should work instead:Here is a code sample that tests this for an interval that starts before the start of DST and ends while in DST. However, I'm not 100% sure how this would behave if the start or end time of the Interval fell exactly on the DST boundary.