简体   繁体   中英

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, if "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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM