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;
/*/*from w w  w. jav  a 2 s  . co  m*/
 * This file is part of the L2J Mobius 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
    }

    public static int min(int value1, int value2, int... values) {
        int min = Math.min(value1, value2);
        for (int value : values) {
            if (min > value) {
                min = value;
            }
        }
        return min;
    }

    public static long min(long value1, long value2, long... values) {
        long min = Math.min(value1, value2);
        for (long value : values) {
            if (min > value) {
                min = value;
            }
        }
        return min;
    }

    public static float min(float value1, float value2, float... values) {
        float min = Math.min(value1, value2);
        for (float value : values) {
            if (min > value) {
                min = value;
            }
        }
        return min;
    }

    public static double min(double value1, double value2, double... values) {
        double min = Math.min(value1, value2);
        for (double value : values) {
            if (min > value) {
                min = value;
            }
        }
        return min;
    }

    /**
     * Re-Maps a value from one range to another.
     * @param input
     * @param inputMin
     * @param inputMax
     * @param outputMin
     * @param outputMax
     * @return The mapped value
     */
    public static int map(int input, int inputMin, int inputMax, int outputMin, int outputMax) {
        input = constrain(input, inputMin, inputMax);
        return (((input - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin;
    }

    /**
     * Re-Maps a value from one range to another.
     * @param input
     * @param inputMin
     * @param inputMax
     * @param outputMin
     * @param outputMax
     * @return The mapped value
     */
    public static long map(long input, long inputMin, long inputMax, long outputMin, long outputMax) {
        input = constrain(input, inputMin, inputMax);
        return (((input - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin;
    }

    /**
     * Re-Maps a value from one range to another.
     * @param input
     * @param inputMin
     * @param inputMax
     * @param outputMin
     * @param outputMax
     * @return The mapped value
     */
    public static double map(double input, double inputMin, double inputMax, double outputMin, double outputMax) {
        input = constrain(input, inputMin, inputMax);
        return (((input - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin;
    }

    /**
     * Constrains a number to be within a range.
     * @param input the number to constrain, all data types
     * @param min the lower end of the range, all data types
     * @param max the upper end of the range, all data types
     * @return input: if input is between min and max, min: if input is less than min, max: if input is greater than max
     */
    public static int constrain(int input, int min, int max) {
        return (input < min) ? min : (input > max) ? max : input;
    }

    /**
     * Constrains a number to be within a range.
     * @param input the number to constrain, all data types
     * @param min the lower end of the range, all data types
     * @param max the upper end of the range, all data types
     * @return input: if input is between min and max, min: if input is less than min, max: if input is greater than max
     */
    public static long constrain(long input, long min, long max) {
        return (input < min) ? min : (input > max) ? max : input;
    }

    /**
     * Constrains a number to be within a range.
     * @param input the number to constrain, all data types
     * @param min the lower end of the range, all data types
     * @param max the upper end of the range, all data types
     * @return input: if input is between min and max, min: if input is less than min, max: if input is greater than max
     */
    public static double constrain(double input, double min, double max) {
        return (input < min) ? min : (input > max) ? max : input;
    }
}

Related

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