⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weeklycalendar.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
字号:
/* * Copyright Juergen Donnerstag (c) 2002, EDS 2002 *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.quartz.impl.calendar;import java.io.Serializable;import java.util.Date;import org.quartz.Calendar;/** * <p> * This implementation of the Calendar excludes a set of days of the week. You * may use it to exclude weekends for example. But you may define any day of * the week. * </p> *  * @see org.quartz.Calendar * @see org.quartz.impl.calendar.BaseCalendar *  * @author Juergen Donnerstag */public class WeeklyCalendar extends BaseCalendar implements Calendar,        Serializable {    // An array to store the week days which are to be excluded.    // java.util.Calendar.MONDAY etc. are used as index.    private boolean[] excludeDays = new boolean[8];    // Will be set to true, if all week days are excluded    private boolean excludeAll = false;    /**     * <p>     * Constructor     * </p>     */    public WeeklyCalendar() {        super();        init();    }    /**     * <p>     * Constructor     * </p>     */    public WeeklyCalendar(Calendar baseCalendar) {        super(baseCalendar);        init();    }    /**     * <p>     * Initialize internal variables     * </p>     */    private void init() {        excludeDays[java.util.Calendar.SUNDAY] = true;        excludeDays[java.util.Calendar.SATURDAY] = true;        excludeAll = areAllDaysExcluded();    }    /**     * <p>     * Get the array with the week days     * </p>     */    public boolean[] getDaysExcluded() {        return excludeDays;    }    /**     * <p>     * Return true, if wday (see Calendar.get()) is defined to be exluded. E. g.     * saturday and sunday.     * </p>     */    public boolean isDayExcluded(int wday) {        return excludeDays[wday];    }    /**     * <p>     * Redefine the array of days excluded. The array must of size greater or     * equal 8. java.util.Calendar's constants like MONDAY should be used as     * index. A value of true is regarded as: exclude it.     * </p>     */    public void setDaysExcluded(boolean[] weekDays) {        if (weekDays == null) return;        excludeDays = weekDays;        excludeAll = areAllDaysExcluded();    }    /**     * <p>     * Redefine a certain day of the week to be excluded (true) or included     * (false). Use java.util.Calendar's constants like MONDAY to determine the     * wday.     * </p>     */    public void setDayExcluded(int wday, boolean exclude) {        excludeDays[wday] = exclude;        excludeAll = areAllDaysExcluded();    }    /**     * <p>     * Check if all week days are excluded. That is no day is included.     * </p>     *      * @return boolean     */    public boolean areAllDaysExcluded() {        if (isDayExcluded(java.util.Calendar.SUNDAY) == false) return false;        if (isDayExcluded(java.util.Calendar.MONDAY) == false) return false;        if (isDayExcluded(java.util.Calendar.TUESDAY) == false) return false;        if (isDayExcluded(java.util.Calendar.WEDNESDAY) == false) return false;        if (isDayExcluded(java.util.Calendar.THURSDAY) == false) return false;        if (isDayExcluded(java.util.Calendar.FRIDAY) == false) return false;        if (isDayExcluded(java.util.Calendar.SATURDAY) == false) return false;        return true;    }    /**     * <p>     * Determine whether the given time (in milliseconds) is 'included' by the     * Calendar.     * </p>     *      * <p>     * Note that this Calendar is only has full-day precision.     * </p>     */    public boolean isTimeIncluded(long timeStamp) {        if (excludeAll == true) return false;        // Test the base calendar first. Only if the base calendar not already        // excludes the time/date, continue evaluating this calendar instance.        if (super.isTimeIncluded(timeStamp) == false) { return false; }        java.util.Calendar cl = java.util.Calendar.getInstance();        cl.setTime(new Date(timeStamp));        int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);        return !(isDayExcluded(wday));    }    /**     * <p>     * Determine the next time (in milliseconds) that is 'included' by the     * Calendar after the given time. Return the original value if timeStamp is     * included. Return 0 if all days are excluded.     * </p>     *      * <p>     * Note that this Calendar is only has full-day precision.     * </p>     */    public long getNextIncludedTime(long timeStamp) {        if (excludeAll == true) return 0;        // Call base calendar implementation first        long baseTime = super.getNextIncludedTime(timeStamp);        if ((baseTime > 0) && (baseTime > timeStamp)) timeStamp = baseTime;        // Get timestamp for 00:00:00        long newTimeStamp = buildHoliday(timeStamp);        java.util.Calendar cl = getJavaCalendar(newTimeStamp);        int wday = cl.get(java.util.Calendar.DAY_OF_WEEK);        if (!isDayExcluded(wday)) return timeStamp; // return the original                                                    // value        while (isDayExcluded(wday) == true) {            cl.add(java.util.Calendar.DATE, 1);            wday = cl.get(java.util.Calendar.DAY_OF_WEEK);        }        return cl.getTime().getTime();    }}

⌨️ 快捷键说明

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