Java Day End checkTimeInRangeWithSkew(Date timeToCheck, Date startDate, Date endDate, int skewInMinutes)

Here you can find the source of checkTimeInRangeWithSkew(Date timeToCheck, Date startDate, Date endDate, int skewInMinutes)

Description

Checks that a date falls in the interval allowing for a certain clock skew expressed in minutes.

License

Apache License

Parameter

Parameter Description
timeToCheck the time to be checked
startDate the start date of the time range
endDate the end date of the time range
skewInMinutes the clock skew in minutes to take into account

Exception

Parameter Description
IllegalArgumentException if passed an illegal time range

Return

true, if the time is in the given range, false otherwise

Declaration

public static boolean checkTimeInRangeWithSkew(Date timeToCheck, Date startDate, Date endDate,
        int skewInMinutes) 

Method Source Code

//package com.java2s;
/**// w  ww . j a  v  a2  s .co m
 * Copyright (c) Istituto Nazionale di Fisica Nucleare, 2006-2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Checks that a date falls in the interval allowing for a certain clock skew
     * expressed in minutes. The interval defined by (startDate, endDate) is
     * modified to be (startDate - skewInMinutes, endDate + skewInMinutes).
     * 
     * @param timeToCheck
     *          the time to be checked
     * @param startDate
     *          the start date of the time range
     * @param endDate
     *          the end date of the time range
     * @param skewInMinutes
     *          the clock skew in minutes to take into account
     * 
     * @throws IllegalArgumentException
     *           if passed an illegal time range
     * @return <code>true</code>, if the time is in the given range,
     *         <code>false</code> otherwise
     */
    public static boolean checkTimeInRangeWithSkew(Date timeToCheck, Date startDate, Date endDate,
            int skewInMinutes) {

        if (startDate.after(endDate) || startDate.equals(endDate)) {
            String msg = String.format(
                    "Illegal time interval: start date must be before end date. [start date: %s, end date: %s]",
                    startDate, endDate);
            throw new IllegalArgumentException(msg);
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        cal.add(Calendar.MINUTE, -skewInMinutes);

        Date skewedStartDate = cal.getTime();

        cal.clear();
        cal.setTime(endDate);
        cal.add(Calendar.MINUTE, skewInMinutes);

        Date skewedEndDate = cal.getTime();

        return skewedEndDate.after(timeToCheck) && skewedStartDate.before(timeToCheck);

    }
}

Related

  1. absoluteDay(Calendar cal)
  2. calculateMaxStartDate(Calendar calendar, int daysAhead)
  3. calcYears(Date startDate, Date endDate)
  4. calenderFromDateString(String dateAndTime)
  5. checkTimePeriod(final Date beginHour, final Date endHour, final Date hour)
  6. countDaysBetween(Date start, Date end)
  7. countMonths(java.util.Date startDate, java.util.Date endDate)
  8. dateBetweens(Date start, Date end)