Java實(shí)現(xiàn)計(jì)算日期(模擬)

插句題外話先,關(guān)于蔡勒公式,網(wǎng)上眾說(shuō)紛紜,這里就不再一一贅述

  • 先看一張簡(jiǎn)圖

    好,現(xiàn)在我們有了基本的了解之后(具體的不用太明細(xì)),可以開(kāi)碼了~請(qǐng)注意這里僅使用模擬法而非蔡勒公式,借這張圖遷移一下思想
import java.lang.reflect.Method;
import java.util.*;


public class Main {
    /**
    * @author: binx6
    * @last-edit-date: 2023.06.14
    */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("請(qǐng)輸入年份:");
int year = scanner.nextInt();
System.out.print("請(qǐng)輸入月份:");
int month = scanner.nextInt();
System.out.print("請(qǐng)輸入日期:");
int day = scanner.nextInt();


    // 判斷該年份是否為閏年
    boolean isLeapYear = isLeapYear(year);

    // 判斷該月份是否存在
    boolean isMonthValid = isMonthValid(month);

    // 判斷該日期是否存在
    boolean isDayValid = isDayValid(year, month, day);

    if (isLeapYear && month == 2 && day == 29) {
        System.out.println(year + "年是閏年,2月29日是存在的");
    } else if (isMonthValid && isDayValid) {
        int week = getWeekday(year, month, day);
        System.out.println(year + "年" + month + "月" + day + "日是星期" + week);
    } else if (!isMonthValid) {
        System.out.println("年份不存在" + month + "月,請(qǐng)重新輸入");
    } else if (!isDayValid) {
        System.out.println(year + "年" + month + "月不存在" + day + "日");
    }
}

public static int getWeekday(int year, int month, int day) {
    int[] monthTable = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    if (month < 3) {
        year--;
    }
    int week = (year + year / 4 - year / 100 + year / 400 + monthTable[month - 1] + day) % 7;
    if (week == 0) {
        week = 7;
    }
    // 假如計(jì)算出來(lái)的星期幾不正確,就將日期往前或往后調(diào)整(糾偏)
    if (week != getDayOfWeek(year, month, day)) {
        if (week < getDayOfWeek(year, month, day)) {
            // 往后調(diào)整
            day++;
        } else {
            // 往前調(diào)整
            day--;
        }
    }
    return week;
}

public static int getDayOfWeek(int year, int month, int day) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, day);
    return cal.get(Calendar.DAY_OF_WEEK);
}

public static boolean isLeapYear(int year) {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        return true;
    }
    return false;
}

public static boolean isMonthValid(int month) {
    try {
        Method method = Calendar.class.getDeclaredMethod("getActualMaximum", int.class);
        Calendar cal = Calendar.getInstance();
        method.invoke(cal, Calendar.MONTH);
        if (month < 1 || month > 12) {
            return false;
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public static boolean isDayValid(int year, int month, int day) {
    try {
        Method method = Calendar.class.getDeclaredMethod("getActualMaximum", int.class);
        Calendar cal = Calendar.getInstance();
        int maxDay = (int) method.invoke(cal, Calendar.DAY_OF_MONTH);
        if (day < 1 || day > maxDay) {
            return false;
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

}
  • 相較于網(wǎng)絡(luò)上經(jīng)典流傳的Switch-Case思路,這里換了種實(shí)現(xiàn)方法,盡可能的達(dá)到不臃腫,熱愛(ài)逆向的人想必對(duì)其也是愛(ài)恨交加吧!

    君可見(jiàn),此處應(yīng)用了反射,我學(xué)Java的日子也并不長(zhǎng),但我覺(jué)得就是要敢于去嘗試,勇于付諸實(shí)踐
  • 寫在章末:有興趣的可以去了解蔡勒公式以及基姆拉爾森公式,也是比較有意思
    問(wèn):為啥出來(lái)寫<水(bushi)>這篇文章?
    答:與萬(wàn)能的群友交流來(lái)的靈感,嗯。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容