Java Data Type How to - Adjust LocalDate to next Sunday








Question

We would like to know how to adjust LocalDate to next Sunday.

Answer

/* w  ww.j  ava 2  s . c o m*/
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

public class Main {
  public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 25); // 2014-02-25

    // next Sunday (2014-03-02)
    LocalDate nextSunday = date.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
    
    System.out.println(nextSunday);
  }
}

The code above generates the following result.