-5

I want to get all weekend between two specific day of month (e.g. from 2018-10-26 to 2018-11-27) using java8 time api. How to do that?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161

1 Answers1

3

Try this function, it will get all Saturday and Sunday from your Time

LocalDate startDate = LocalDate.of(2018, 10, 26);
LocalDate endDate = LocalDate.of(2018, 11, 27);
while (startDate.isBefore(endDate)) {
    if (startDate.getDayOfWeek().equals(DayOfWeek.SATURDAY) || startDate.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
        System.out.println(startDate + ":" + startDate.getDayOfWeek());
    }
    startDate = startDate.plusDays(1);
}
  • 2
    Please always give explanation to your answer even when you provide codes. – holydragon Nov 27 '18 at 03:13
  • @holydragon this code is self-explanatory, no explanation needed here – Kartik Nov 27 '18 at 03:31
  • For a twist on this Answer, you could use [`LocalDate::datesUntil`](https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#datesUntil(java.time.LocalDate)) to produce a `Stream` to work with. – Basil Bourque Dec 11 '18 at 03:41