Java星期的复选运算

基本实现原理:

Java星期的复选运算_第1张图片

用一个字节就可以记录选中状态,其中1表示选中,0表示未选中,位置代表星期的位置。

代码实现如下:

/**
 * Days of week code as a single int. 
 * 0x00: no day   
 * 0x01: Monday
 * 0x02: Tuesday
 * 0x04: Wednesday
 * 0x08: Thursday
 * 0x10: Friday
 * 0x20: Saturday
 * 0x40: Sunday
 */
public final class DaysOfWeek {

    private static int[] DAY_MAP = new int[] {
        Calendar.MONDAY,
        Calendar.TUESDAY,
        Calendar.WEDNESDAY,
        Calendar.THURSDAY,
        Calendar.FRIDAY,
        Calendar.SATURDAY,
        Calendar.SUNDAY,
    };

    // Bitmask of all repeating days
    private int mDays;

    DaysOfWeek(int days) {
        mDays = days;
    }

    public String toString(boolean showNever) {
        StringBuilder ret = new StringBuilder();

        // no days
        if (mDays == 0) {
            return showNever ? "一律不" : "";
        }

        // every day
        if (mDays == 0x7f) {
            return "每天";
        }

        // count selected days
        int dayCount = 0, days = mDays;
        while (days > 0) {
            if ((days & 1) == 1) dayCount++;
            days >>= 1;
        }

        // short or long form?
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] dayList = (dayCount > 1) ?
                dfs.getShortWeekdays() :
                dfs.getWeekdays();

        // selected days
        for (int i = 0; i < 7; i++) {
            if ((mDays & (1 << i)) != 0) {
                ret.append(dayList[DAY_MAP[i]]);
                dayCount -= 1;
                if (dayCount > 0) ret.append(", ");
            }
        }
        return ret.toString();
    }

    // 判断第几位是否被选中
    private boolean isSet(int day) {
     /*
      * 如day为第三位
      * (1 << day) 即为:00000100
      * 00000100 与 mDays,只有当Days第三位为1时,才不等于0
      */
        return ((mDays & (1 << day)) > 0);
    }

    public void set(int day, boolean set) {
        if (set) {
            mDays |= (1 << day);
        } else {
            mDays &= ~(1 << day);
        }
    }

    public void set(DaysOfWeek dow) {
        mDays = dow.mDays;
    }

    public int getCoded() {
        return mDays;
    }

    // Returns days of week encoded in an array of booleans.
    public boolean[] getBooleanArray() {
        boolean[] ret = new boolean[7];
        for (int i = 0; i < 7; i++) {
            ret[i] = isSet(i);
        }
        return ret;
    }

    public boolean isRepeatSet() {
        return mDays != 0;
    }

    /**
     * returns number of days from today until next alarm
     * @param c must be set to today
     */
    public int getNextAlarm(Calendar c) {
        if (mDays == 0) {
            return -1;
        }

        int today = (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
        int day = 0;
        int dayCount = 0;
        for (; dayCount < 7; dayCount++) {
            // 今天加上将要发生的天数(1-7)
            day = (today + dayCount) % 7;
            if (isSet(day)) {
                break;
            }
        }
        return dayCount;
    }
}

调用方法:

DaysOfWeek daysOfWeek = new DaysOfWeek(0x00);    // 初始化一个选中状态
daysOfWeek.set(0, true);    // 设置星期一选中
daysOfWeek.set(1, true);    // 设置星期二选中
daysOfWeek.set(0, false);   // 取消星期一的选中

// 获取选中的星期,第0位代表星期一
boolean[] weekBool = daysOfWeek.getBooleanArray();

你可能感兴趣的:(java,星期-位运算)