如何使用Java中的Joda日期/时间库获取“本周”的日期/时间范围?

时间:2011-06-29 19:40:40

标签: java datetime jodatime

假设您可以计算“今天”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.

你将如何为“本周”做类似的事情。

所以,如果“今天”是2011年6月29日20:38,“本周”将是

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

1 个答案:

答案 0 :(得分:4)

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

(注意:工作日是从1开始的)