Within a month, this is easy - something like:
LocalDate endOfPreviousMonth = date.withDayOfMonth(1).minusDays(1);
This is a bit more difficult during the week. You can do it:
LocalDate previousSunday = date.withDayOfWeek(DateTimeConstants.MONDAY) .minusDays(1);
... but it is not clear to me whether withDayOfWeek
always go the previous Monday or next Monday. (This is what I'm going to find out in Noda Time and give options ...)
Instead, you can try the following:
// Go back 1 day for Monday, 2 days for Tuesday etc LocalDate previousSunday = date.minusDays(date.getDayOfWeek());
Once you finish a completed week or month, you can easily get to the start:
LocalDate startOfPreviousMonth = endOfPreviousMonth.withDayOfMonth(1); LocalDate startOfPreviousWeek = previousSunday.minusDays(6);
source share