List of usage examples for java.time.temporal TemporalAdjusters nextOrSame
public static TemporalAdjuster nextOrSame(DayOfWeek dayOfWeek)
From source file:Main.java
/** * Gets the next or same closest date from the specified days in * {@code daysOfWeek List} at specified {@code hour} and {@code min}. * /*from www . ja v a2s .co m*/ * @param daysOfWeek * the days of week * @param hour * the hour * @param min * the min * @return the next or same date from the days of week at specified time * @throws IllegalArgumentException * if the {@code daysOfWeek List} is empty. */ public static LocalDateTime getNextClosestDateTime(List<DayOfWeek> daysOfWeek, int hour, int min) throws IllegalArgumentException { if (daysOfWeek.isEmpty()) { throw new IllegalArgumentException("daysOfWeek should not be empty."); } final LocalDateTime dateNow = LocalDateTime.now(); final LocalDateTime dateNowWithDifferentTime = dateNow.withHour(hour).withMinute(min).withSecond(0); // @formatter:off return daysOfWeek.stream().map(d -> dateNowWithDifferentTime.with(TemporalAdjusters.nextOrSame(d))) .filter(d -> d.isAfter(dateNow)).min(Comparator.naturalOrder()) .orElse(dateNowWithDifferentTime.with(TemporalAdjusters.next(daysOfWeek.get(0)))); // @formatter:on }