根据年月日获取星期

蔡勒公式

实现:

public class DayOfWeek {
    private static final int[] MAX_DAY = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    /**
     * 根据日期计算星期
     *
     * @param year  年
     * @param month 月
     * @param day   日
     * @return 0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
     */
    public static int dayOfWeek(int year, int month, int day) {
        if (month > 12 || month < 1) {
            throw new IllegalArgumentException(String.format("月份范围为:1-12,您的输入为%d", month));
        }
        if (!validMaxDay(year, month, day)) {
            throw new IllegalArgumentException(String.format("日期超出该月最大天数,您的输入为%d年%d月%d号", year, month, day));
        }

        int y = year;
        int m = month;
        int d = day;
        if (m <= 2) {
            m += 12;
            y -= 1;
        }

        int c;
        if (y < 0) {
            c = (y / 100) - 1;
            y = y % 100 + 100;
        } else {
            c = y / 100;
            y = y % 100;
        }

        int w;
        if (year < 1582 || (year == 1582 && month < 10) || (year == 1582 && month == 10 && day <= 4)) {
            // 1582年10月04日及以前
            w = (y + y / 4 + c / 4 - 2 * c + (13 * (m + 1)) / 5 + d + 2) % 7;
        } else if (year > 1582 || (year == 1582 && month > 10) || (year == 1582 && month == 10 && day >= 15)) {
            // 1582年10月15日及以后
            w = (y + y / 4 + c / 4 - 2 * c + (13 * (m + 1)) / 5 + d - 1) % 7;
        } else {
            throw new IllegalArgumentException("1582年10月5号 到 1582年10月14号 不存在");
        }
        return w;
    }

    /**
     * 校验每个月的最大天数是否符合规则
     *
     * @param year  年
     * @param month 月
     * @param day   日
     * @return true-最大天数是正确的,false-错误的最大天数
     */
    private static boolean validMaxDay(int year, int month, int day) {
        if (month != 2) {
            return day > 0 && day <= MAX_DAY[month - 1];
        } else {
            int februaryMaxDay = (year % 100 == 0 ? year % 400 : year % 4) == 0 ? 29 : 28;
            return day > 0 && day <= februaryMaxDay;
        }
    }

    public static void main(String[] args) {
        int i = dayOfWeek(1582, 10, 1);
        System.out.println(i);
        i = dayOfWeek(1582, 10, 16);
        System.out.println(i);
        i = dayOfWeek(1582, 10, 15);
        System.out.println(i);
        i = dayOfWeek(1582, 10, 4);
        System.out.println(i);
        i = dayOfWeek(1582, 10, 3);
        System.out.println(i);
        System.out.println("--------");
        i = dayOfWeek(0, 1, 2);
        System.out.println(i);
        i = dayOfWeek(0, 1, 1);
        System.out.println(i);
        i = dayOfWeek(-1, 12, 31);
        System.out.println(i);
        i = dayOfWeek(-1, 12, 30);
        System.out.println(i);
        System.out.println("--------");
        i = dayOfWeek(1900, 1, 1);
        System.out.println(i);
        i = dayOfWeek(1900, 1, 2);
        System.out.println(i);
        i = dayOfWeek(1970, 1, 1);
        System.out.println(i);
        i = dayOfWeek(1970, 1, 2);
        System.out.println(i);
        System.out.println("***************");
        i = dayOfWeek(2000, 2, 29);
        System.out.println(i);
        i = dayOfWeek(2004, 2, 29);
        System.out.println(i);
        i = dayOfWeek(2005, 2, 28);
        System.out.println(i);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值