Java Day of Week getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)

Here you can find the source of getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)

Description

Gets the next or same closest date from the specified days in daysOfWeek Array at specified hour and min .

License

Open Source License

Parameter

Parameter Description
daysOfWeek the days of week
hour the hour
min the min

Exception

Parameter Description
IllegalArgumentException if the daysOfWeek Array is empty.

Return

the next or same date from the days of week at specified time

Declaration

public static LocalDateTime getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)
        throws IllegalArgumentException 

Method Source Code

//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
    }
}

Related

  1. getLastWeekDay(int weekDay, Date end)
  2. getMondayFirstOfWeek(Date baseDate)
  3. getMondayOfThisWeek(Date date)
  4. getMondayOfWeek(Date date)
  5. getNext(final java.util.Date date, final int dayofweek)
  6. getNextClosestDateTime(DayOfWeek[] daysOfWeek, int hour, int min)
  7. getNextDay(long date, int startOfWeek)
  8. getNextWeekDay(Date startDate, int day)
  9. getSatDayOfWeek(Date date)