GitHub
The Java Version Almanac
javaalmanac.io
Feedback on this page?

Friday 13th

To find all Friday 13th we iterate over a range of YearMonth and check whether the LocalDate with day 13 is a Friday.

Since Java 8

import java.time.DayOfWeek; import java.time.LocalDate; import java.time.YearMonth; import java.util.stream.Stream; public class Friday13th { public static void main(String[] args) { YearMonth start = YearMonth.of(2000, 1); YearMonth end = YearMonth.of(2030, 12); Stream.iterate(start, m -> m.isBefore(end), m -> m.plusMonths(1)) // .map(m -> m.atDay(13)) // .filter(Friday13th::isFriday) // .map("%ta %<s"::formatted) // .forEach(System.out::println); } static boolean isFriday(LocalDate date) { return DayOfWeek.FRIDAY.equals(date.getDayOfWeek()); } }

This snippet at GitHub