Search code examples
javadatetimejava-8dayofweeklocaldate

How to find first sunday of next month in Java 8?


I searched on StackOverflow for the same, but all the answers are for legacy java versions. I did not find any of the answers with Java 8 Data and Time utility. Can anybody help me out for the same?


Solution

  • I find out a way using LocalDate and TemporalAdjuster's with() method as follows:

    LocalDate firstSundayOfNextMonth = LocalDate
                  .now()
                  .with(firstDayOfNextMonth())
                  .with(nextOrSame(DayOfWeek.SUNDAY));