开发者

How do you get the date/time range for “this week” using the Joda date/time library in Java?

Assuming you can calculate the date/time range for "today" by following Jon Skeet's advice:

LocalDate today = now.toLocalDate();
LocalDate tomorrow = today.plusDays(1);

DateTime startOfToday = today.toDateTimeAtStartOfDay(now.getZone());
DateTime startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(now.getZone());

Then check if startOfToday <= time < startOfTomorrow for any particular time.

How would you do something similar for "this week".

So, i开发者_StackOverflow中文版f "today" is 20:38 29/06/2011, "this week" would be

20:38 22/06/2011 - 20:38 29/06/2011


DateTime startOfWeek = dateTime.minusDays(dateTime.dayOfWeek().get() - 1);
DateTime endOfWeek = dateTime.plusDays(7 - dateTime.dayOfWeek().get());

(Note: week days are 1-based)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜