Java: 今月の 06:00 と 18:00 を列挙

メモ。

		LocalDate today = LocalDate.now();
		YearMonth thisMonth = YearMonth.from(today);
		final LocalDate firstDay = thisMonth.atDay(1);
		final LocalDate lastDay = thisMonth.atEndOfMonth();
		System.out.printf("今月: %s - %s%n", firstDay, lastDay);

		Iterator<LocalDate> dateIterator = new Iterator<LocalDate>() {
			private LocalDate nextDate = firstDay.isAfter(lastDay) ? null : firstDay;

			@Override
			public LocalDate next() {
				LocalDate currentDate = nextDate;
				nextDate = currentDate.plusDays(1);
				if (nextDate.isAfter(lastDay)) {
					nextDate = null;
				}
				return currentDate;
			}

			@Override
			public boolean hasNext() {
				return nextDate != null;
			}
		};

		Stream<LocalDate> dateStream = StreamSupport.stream(
				Spliterators.spliteratorUnknownSize(dateIterator, ORDERED
						| DISTINCT | SORTED | NONNULL | IMMUTABLE), false);
		List<LocalTime> times = Arrays.asList(LocalTime.parse("06:00:00"), LocalTime.parse("18:00:00"));
		dateStream.flatMap(date -> times.stream().map(time -> date.atTime(time)))
			.forEach(System.out::println);

出力例。

今月: 2014-07-01 - 2014-07-31
2014-07-01T06:00
2014-07-01T18:00
2014-07-02T06:00
2014-07-02T18:00
(中略)
2014-07-30T06:00
2014-07-30T18:00
2014-07-31T06:00
2014-07-31T18:00