开发者

Better way to check if a joda.time.Interval spans exactly 1 calendar week (accounting for daylight savings etc)

开发者 https://www.devze.com 2023-03-02 20:34 出处:网络
Is there a better way of doing this? boolean oneCalendarWeek = interval.getStart().plusWeeks(1).equals( interval.getEnd() );

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开发者_JAVA技巧.toPeriod().equals( Weeks.ONE );


From the comments:

i really want to know if the api supports something like my second example which i think is clearer than the first

While the example using Weeks.ONE does not work (since Period.equals() first checks if the two Period instances support the same number of fields, and Weeks.ONE only supports one field), this should work instead:

boolean oneCalendarWeek = interval.toPeriod().equals( Period.weeks(1) );

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号