Skip to content
Ruvia02 edited this page Jun 7, 2024 · 1 revision

public static int getWeekday(int year, int month, int day) { // 验证输入合法性 int[] MonthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0) { MonthDays[1] = 29; } if (year < 0 || month < 0 || day < 0) { return 0; } if (year > 10000) { return -1; } else if (month > 12) { return -2; } else if (day > MonthDays[month - 1]) { return -3; } // 开始计算 int totalDays = 0; for (int i = 1; i < year; i++) { if (((i % 4 == 0) && (i % 100 != 0)) || i % 400 == 0) { totalDays += 366; } else { totalDays += 365; } } for (int i = 1; i < month; i++) { if (i == 2) { if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0) { MonthDays[1] = 29; } } totalDays += MonthDays[i - 1]; } totalDays += day; int tempDay = totalDays % 7; if (tempDay == 0) { tempDay = 7; } return tempDay; }

Clone this wiki locally