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?
Asked
Active
Viewed 143 times
-5
-
3What have you tried? Where are you stuck? – shmosel Nov 27 '18 at 01:49
-
`Stream.iterate(LocalDate.parse("2018-10-26"), LocalDate.parse("2018-11-27")::isAfter, Duration.ofDays(1)::addTo).filter(this::isWeekday)` – shmosel Nov 27 '18 at 02:03
-
Duplicate of [*In Java, get all weekend dates in a given month*](https://stackoverflow.com/q/3272454/642706) – Basil Bourque Dec 11 '18 at 03:42
-
**Search Stack Overflow** thoroughly before posting. Assume any basic date-time Question has already been asked and answered. – Basil Bourque Dec 11 '18 at 03:43
1 Answers
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);
}

Quang Nghĩa
- 41
- 5
-
2Please always give explanation to your answer even when you provide codes. – holydragon Nov 27 '18 at 03:13
-
-
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