Here you can find the source of getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)
Parameter | Description |
---|---|
daysOfWeek | the days of week |
hour | the hour |
min | the min |
Parameter | Description |
---|---|
IllegalArgumentException | if the daysOfWeek Array is empty. |
public static LocalDateTime getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min) throws IllegalArgumentException
//package com.java2s; /*// w w w . ja v a 2s . c o m * This file is part of the L2J Olivia project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.temporal.TemporalAdjusters; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { /** * Gets the next or same closest date from the specified days in {@code daysOfWeek Array} at specified {@code hour} and {@code min}. * @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 Array} is empty. */ public static LocalDateTime getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min) throws IllegalArgumentException { return getNextClosestDateTime(Arrays.asList(daysOfWeek), hour, min); } /** * Gets the next or same closest date from the specified days in {@code daysOfWeek List} at specified {@code hour} and {@code min}. * @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 } }