1185. Day of the Week

QUESTION:

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}.

Example 1:

Input: day = 31, month = 8, year = 2019 Output: “Saturday” Example 2:

Input: day = 18, month = 7, year = 1999 Output: “Sunday” Example 3:

Input: day = 15, month = 8, year = 1993 Output: “Sunday”

Constraints:

The given dates are valid dates between the years 1971 and 2100.

EXPLANATION:

emmm,好吧,最近确实在打酱油。
这个题目只要查一下calendar的api就可以很快的知道有一个属性叫DAY_OF_WEEK,那么直接设置到对应的时间,获取这一个树形就可以了。根据leetcode的测试方式,其实可以将days弄成静态成员变量,这样就可以节省每次调用函数都会重新创建对象的消耗。

SOLUTION:

class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        Calendar c =  Calendar.getInstance();
        c.set(year,month-1,day);
        return days[c.get(Calendar.DAY_OF_WEEK)-1];
    }
}