dailycalendar.java

来自「Quartz 是个开源的作业调度框架」· Java 代码 · 共 803 行 · 第 1/3 页

JAVA
803
字号
     * @param baseCalendar          the base calendar for this calendar instance
     *                              – see {@link BaseCalendar} for more 
     *                              information on base calendar functionality
     * @param rangeStartingCalendar a java.util.Calendar representing the 
     *                              starting time for the time range
     * @param rangeEndingCalendar   a java.util.Calendar representing the ending
     *                              time for the time range
     */
    public DailyCalendar(String name,
                         org.quartz.Calendar baseCalendar,
                         Calendar rangeStartingCalendar,
                         Calendar rangeEndingCalendar) {
        super(baseCalendar);
        this.name = name;
        setTimeRange(rangeStartingCalendar, rangeEndingCalendar);
    }

    /**
     * Create a <CODE>DailyCalendar</CODE> with a time range defined by the
     * specified values and no <CODE>baseCalendar</CODE>. The values are 
     * subject to the following considerations:
     * <UL><LI>Only the time-of-day portion of the specified values will be
     *         used</LI>
     *     <LI>The starting time must be before the ending time of the defined
     *         time range. Note this means that a time range may not cross
     *         daily boundaries (10PM - 2AM). <I>(because only time value are
     *         are used, it is possible for the two values to represent a valid
     *         time range and <CODE>rangeStartingTime &gt; 
     *         rangeEndingTime</CODE>)</I></LI>  
     * </UL> 
     * 
     * @param name                      the name for the 
     *                                  <CODE>DailyCalendar</CODE>
     * @param rangeStartingTimeInMillis a long representing the starting time 
     *                                  for the time range
     * @param rangeEndingTimeInMillis   a long representing the ending time for
     *                                  the time range
     */
    public DailyCalendar(String name,
                         long rangeStartingTimeInMillis,
                         long rangeEndingTimeInMillis) {
        super();
        this.name = name;
        setTimeRange(rangeStartingTimeInMillis, 
        		     rangeEndingTimeInMillis);
    }

    /**
     * Create a <CODE>DailyCalendar</CODE> with a time range defined by the
     * specified values and the specified <CODE>baseCalendar</CODE>. The values
     * are subject to the following considerations:
     * <UL><LI>Only the time-of-day portion of the specified values will be
     *         used</LI>
     *     <LI>The starting time must be before the ending time of the defined
     *         time range. Note this means that a time range may not cross
     *         daily boundaries (10PM - 2AM). <I>(because only time value are
     *         are used, it is possible for the two values to represent a valid
     *         time range and <CODE>rangeStartingTime &gt; 
     *         rangeEndingTime</CODE>)</I></LI>  
     * </UL> 
     * 
     * @param name                      the name for the 
     *                                  <CODE>DailyCalendar</CODE>
     * @param baseCalendar              the base calendar for this calendar
     *                                  instance &ndash; see {@link 
     *                                  BaseCalendar} for more information on 
     *                                  base calendar functionality
     * @param rangeStartingTimeInMillis a long representing the starting time 
     *                                  for the time range
     * @param rangeEndingTimeInMillis   a long representing the ending time for
     *                                  the time range
     */
    public DailyCalendar(String name,
                         org.quartz.Calendar baseCalendar,
                         long rangeStartingTimeInMillis,
                         long rangeEndingTimeInMillis) {
        super(baseCalendar);
        this.name = name;
        setTimeRange(rangeStartingTimeInMillis,
        		     rangeEndingTimeInMillis);
    }

    /**
     * Returns the name of the <CODE>DailyCalendar</CODE>
     * 
     * @return the name of the <CODE>DailyCalendar</CODE>
     */
    public String getName() {
        return name;
    }

    /**
     * Determines whether the given time (in milliseconds) is 'included' by the
     * <CODE>BaseCalendar</CODE>
     * 
     * @param timeInMillis the date/time to test
     * @return a boolean indicating whether the specified time is 'included' by
     *         the <CODE>BaseCalendar</CODE>
     */
    public boolean isTimeIncluded(long timeInMillis) {        
        if ((getBaseCalendar() != null) && 
        		(getBaseCalendar().isTimeIncluded(timeInMillis) == false)) {
        	return false;
        }
        
        long startOfDayInMillis = getStartOfDayInMillis(timeInMillis);
        long endOfDayInMillis = getEndOfDayInMillis(timeInMillis);
        long timeRangeStartingTimeInMillis = 
        	getTimeRangeStartingTimeInMillis(timeInMillis);
        long timeRangeEndingTimeInMillis = 
        	getTimeRangeEndingTimeInMillis(timeInMillis);
        if (!invertTimeRange) {
	        if ((timeInMillis > startOfDayInMillis && 
	        		timeInMillis < timeRangeStartingTimeInMillis) ||
	            (timeInMillis > timeRangeEndingTimeInMillis && 
	            	timeInMillis < endOfDayInMillis)) {
	        	
	        		return true;
	        } else {
	        	return false;
	        }
        } else {
        	if ((timeInMillis >= timeRangeStartingTimeInMillis) &&
        			(timeInMillis <= timeRangeEndingTimeInMillis)) {
        		return true;
        	} else {
        		return false;
        	}
        }
    }

    /**
     * Determines the next time included by the <CODE>DailyCalendar</CODE>
     * after the specified time.
     * 
     * @param timeInMillis the initial date/time after which to find an 
     *                     included time
     * @return the time in milliseconds representing the next time included
     *         after the specified time.
     */
    public long getNextIncludedTime(long timeInMillis) {
        long nextIncludedTime = timeInMillis + oneMillis;
        
        while (!isTimeIncluded(nextIncludedTime)) {
        	if (!invertTimeRange) {
	        	//If the time is in a range excluded by this calendar, we can
	        	// move to the end of the excluded time range and continue 
	        	// testing from there. Otherwise, if nextIncludedTime is 
        		// excluded by the baseCalendar, ask it the next time it 
        		// includes and begin testing from there. Failing this, add one
        		// millisecond and continue testing.
	        	if ((nextIncludedTime >= 
	        			getTimeRangeStartingTimeInMillis(nextIncludedTime)) && 
					(nextIncludedTime <= 
						getTimeRangeEndingTimeInMillis(nextIncludedTime))) {
	        		
	        		nextIncludedTime = 
	        			getTimeRangeEndingTimeInMillis(nextIncludedTime) + 
							oneMillis;
	        	} else if ((getBaseCalendar() != null) && 
	        			(!getBaseCalendar().isTimeIncluded(nextIncludedTime))){
	        		nextIncludedTime = 
	        			getBaseCalendar().getNextIncludedTime(nextIncludedTime);
	        	} else {
	        		nextIncludedTime++;
	        	}
        	} else {
	        	//If the time is in a range excluded by this calendar, we can
	        	// move to the end of the excluded time range and continue 
	        	// testing from there. Otherwise, if nextIncludedTime is 
        		// excluded by the baseCalendar, ask it the next time it 
        		// includes and begin testing from there. Failing this, add one
        		// millisecond and continue testing.
        		if (nextIncludedTime < 
        				getTimeRangeStartingTimeInMillis(nextIncludedTime)) {
        			nextIncludedTime = 
        				getTimeRangeStartingTimeInMillis(nextIncludedTime);
        		} else if (nextIncludedTime > 
        				getTimeRangeEndingTimeInMillis(nextIncludedTime)) {
        			//(move to start of next day)
        			nextIncludedTime = getEndOfDayInMillis(nextIncludedTime);
        			nextIncludedTime += 1l; 
        		} else if ((getBaseCalendar() != null) && 
	        			(!getBaseCalendar().isTimeIncluded(nextIncludedTime))){
	        		nextIncludedTime = 
	        			getBaseCalendar().getNextIncludedTime(nextIncludedTime);
	        	} else {
	        		nextIncludedTime++;
	        	}
        	}
        }
        
        return nextIncludedTime;
    }

    /**
     * Returns the start time of the time range (in milliseconds) of the day 
     * specified in <CODE>timeInMillis</CODE>
     * 
     * @param timeInMillis a time containing the desired date for the starting
     *                     time of the time range.
     * @return a date/time (in milliseconds) representing the start time of the
     *         time range for the specified date.
     */
    public long getTimeRangeStartingTimeInMillis(long timeInMillis) {
        Calendar rangeStartingTime = Calendar.getInstance();
        rangeStartingTime.setTimeInMillis(timeInMillis);
        rangeStartingTime.set(Calendar.HOUR_OF_DAY, rangeStartingHourOfDay);
        rangeStartingTime.set(Calendar.MINUTE, rangeStartingMinute);
        rangeStartingTime.set(Calendar.SECOND, rangeStartingSecond);
        rangeStartingTime.set(Calendar.MILLISECOND, rangeStartingMillis);
        return rangeStartingTime.getTimeInMillis();
    }

    /**
     * Returns the end time of the time range (in milliseconds) of the day
     * specified in <CODE>timeInMillis</CODE>
     * 
     * @param timeInMillis a time containing the desired date for the ending
     *                     time of the time range.
     * @return a date/time (in milliseconds) representing the end time of the
     *         time range for the specified date.
     */
    public long getTimeRangeEndingTimeInMillis(long timeInMillis) {
        Calendar rangeEndingTime = Calendar.getInstance();
        rangeEndingTime.setTimeInMillis(timeInMillis);
        rangeEndingTime.set(Calendar.HOUR_OF_DAY, rangeEndingHourOfDay);
        rangeEndingTime.set(Calendar.MINUTE, rangeEndingMinute);
        rangeEndingTime.set(Calendar.SECOND, rangeEndingSecond);
        rangeEndingTime.set(Calendar.MILLISECOND, rangeEndingMillis);
        return rangeEndingTime.getTimeInMillis();
    }

    /**
     * Indicates whether the time range represents an inverted time range (see
     * class description).
     * 
     * @return a boolean indicating whether the time range is inverted
     */
    public boolean getInvertTimeRange() {
    	return invertTimeRange;
    }
    
    /**
     * Indicates whether the time range represents an inverted time range (see
     * class description).
     * 
     * @param flag the new value for the <CODE>invertTimeRange</CODE> flag.
     */
    public void setInvertTimeRange(boolean flag) {
    	this.invertTimeRange = flag;
    }
    
    /**
     * Returns a string representing the properties of the 
     * <CODE>DailyCalendar</CODE>
     * 
     * @return the properteis of the DailyCalendar in a String format
     */
    public String toString() {
        long todayInMillis = Calendar.getInstance().getTimeInMillis();
        NumberFormat numberFormatter = NumberFormat.getNumberInstance();
        numberFormatter.setMaximumFractionDigits(0);
        numberFormatter.setMinimumIntegerDigits(2);
        StringBuffer buffer = new StringBuffer();
        buffer.append(getName());
        buffer.append(": base calendar: [");
        if (getBaseCalendar() != null) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?