I have the following code for getting the last Sunday before the current date:
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_YEAR, cale
java.time.temporal.TemporalAdjuster
This can be easily achieved by a TemporalAdjuster implementation found in TemporalAdjusters along with the DayOfWeek enum. Specifically, previous(DayOfWeek dayOfWeek).
LocalDate
.now()
.with(
TemporalAdjusters.previous( DayOfWeek.SUNDAY )
) ;
If today is Sunday and you would like the result to be today rather than a week ago, call previousOrSame(DayOfWeek dayOfWeek).
LocalDate
.now()
.with(
TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY )
) ;
These classes are built into Java 8 and later, and built into Android 26 and later. For Java 6 & 7, most of the functionality is back-ported in ThreeTen-Backport. For earlier Android, see ThreeTenABP.