【数学】C021_一周中的第几天(Calendar日期类 | 常规逻辑)

一、题目描述

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"

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

二、题解

(1) Calendar 日期类

/**
 * 日期类
 * 注意:Calendar.MONTH,和 DAY_OF_WEEK,1对应的是星期日,依次类退
 * @param day
 * @param month
 * @param year
 * @return
 */
public String dayOfTheWeek(int day, int month, int year) {

  Calendar c = Calendar.getInstance();
  c.set(Calendar.YEAR, year);
  c.set(Calendar.MONTH, month-1);
  c.set(Calendar.DAY_OF_MONTH, day);
  String[] dateInWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  return dateInWeek[c.get(Calendar.DAY_OF_WEEK)-1];
}

(2) 常规逻辑


发布了300 篇原创文章 · 获赞 48 · 访问量 8076

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/103980460