fork download
  1. import java.time.DayOfWeek;
  2. import java.time.LocalDate;
  3. import java.time.ZoneId;
  4. import java.time.temporal.TemporalAdjusters;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. // Replace JVM's ZoneId, ZoneId.systemDefault() with the applicable one e.g.
  9. // ZoneId.of("America/Los_Angeles")
  10. LocalDate today = LocalDate.now(ZoneId.systemDefault());
  11.  
  12. // Next Sunday
  13. LocalDate nextSun = today.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
  14. System.out.println(nextSun);
  15.  
  16. // Same (if it's Sunday today) of next Sunday
  17. LocalDate sameOrNextSun = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
  18. System.out.println(sameOrNextSun);
  19.  
  20. // Previous Sunday
  21. LocalDate previousSun = today.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY));
  22. System.out.println(previousSun);
  23.  
  24. // Same (if it's Sunday today) of previous Sunday
  25. LocalDate sameOrPreviousSun = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
  26. System.out.println(sameOrPreviousSun);
  27. }
  28. }
Success #stdin #stdout 0.1s 48948KB
stdin
Standard input is empty
stdout
2021-07-25
2021-07-18
2021-07-11
2021-07-18