leetcode-放松题-一周中的第几天
package algorithm.dayOfTheWeek;
/**
* 1185. 一周中的第几天
* 给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。
*
* 输入为三个整数:day、month 和 year,分别表示日、月、年。
*
* 您返回的结果必须是这几个值中的一个 {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}。
*
*
*
* 示例 1:
*
* 输入:day = 31, month = 8, year = 2019
* 输出:"Saturday"
* 示例 2:
*
* 输入:day = 18, month = 7, year = 1999
* 输出:"Sunday"
* 示例 3:
*
* 输入:day = 15, month = 8, year = 1993
* 输出:"Sunday"
*
*
* 提示:
*
* 给出的日期一定是在 1971 到 2100 年之间的有效日期。
*/
public class dayOfTheWeek {
public static String dayOfTheWeek(int day, int month, int year) {
String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
//计算到今年多少天,加上闰年的
int days = 365 * (year-1971)+(year-1969)/4;
//计算这个月的
for (int i = 0; i < month-1; i++) {
days+=monthDays[i];
}
if(year%400==0 ||(year %4==0 &&year%100!=0)){
days+=1;
}
//计算这个月过了几天
days+=day;
//1971.1.1是星期五就行了,+3转化成周一
return week[(days+3)%7];
}
public static void main(String[] args) {
int day = 17, month = 1, year = 2022;
System.out.println(dayOfTheWeek(day,month,year));
}
}
不恋尘世浮华,不写红尘纷扰