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
This question and answers originated from www.stackoverflow.com
Question by (6/29/2011 7:40:39 PM)

Answer

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

(Note: week days are 1-based)

Answer by

Find More Answers
Related Topics  java  datetime  jodatime
Related Questions