2023-12-30
文章目錄
- 一周中的第幾天
- 方法一:模擬
- 思路
- 步驟
- 方法二:調用庫函數
- 方法三:調用庫函數
- [1154. 一年中的第幾天](https://leetcode.cn/problems/day-of-the-year/)
- 方法一:直接計算
- 思路:
- 方法二:調用庫函數
- 思路
一周中的第幾天
?
- 提示:給出的日期一定是在
1971
到2100
年之間的有效日期。
方法一:模擬
思路
-
1.可以根據1970年的最后一天(周四),計算出和輸入日期間隔了幾天(ans+之前的年份天數和+當前的月份天數和+輸日的天數)
-
2.求出具體的天數之后進行取模,得到具體的星期
步驟
1.定義一個字符串類型的數組,大小為7,記錄星期字典
定義一個int類型的數組,大小為12,記錄12個月份分別對應的天數(閏年進行判斷)
2.定義一個初始天數ans=4(從周四開始)
3.從1971年開始遍歷,直到輸入的年份,判斷當前年是否為閏年,初始值加上對應的天數
4.遍歷月份直到輸入月份,二月的情況根據是否是閏年來加上額外的一天
5.最后加上輸入的天數,所求之和取模
static String[] week =new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};//每周對應的星期static int[] nums = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//每個月對應的天數public String dayOfTheWeek(int day, int month, int year) {int ans = 4;//初始值為4,從周四開始for (int i = 1971; i < year; i++) {boolean isLeapYear = (i % 4 == 0 && i % 100 != 0) || i % 400 == 0;//判斷是否為閏年ans += isLeapYear ? 366 : 365;//是閏年加366,不是加355}for (int i = 1; i < month; i++) {ans += nums[i - 1];//之前的月份數之和if (i == 2 && ((year % 4 == 0 && year% 100 != 0) || year % 400 == 0)){ans++;//如果當前是閏年,單獨加1}}ans += day;//加上輸入的天數return week[ans % 7];//求模返回對應星期}
方法二:調用庫函數
1.使用LocalDate.of(year, month, day)
創建了一個LocalDate
對象date
,表示給定的日期。LocalDate
是Java 8中新增的日期類,用于處理日期相關操作。(Java8新增)
2.通過調用date.getDayOfWeek()
方法獲取該日期的星期幾信息,getDayOfWeek()
返回的是一個DayOfWeek
枚舉類型,表示星期幾。
3.最后,通過調用getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH)
方法,將星期幾信息以全名的形式返回,使用英語的地區設置。
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.*;class Solution {public String dayOfTheWeek2(int day, int month, int year) {LocalDate date = LocalDate.of(year, month, day);//創建了一個`LocalDate`對象`date`,表示給定的日期return date.getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH);//方法獲取該日期的星期幾信息//將星期幾信息以全名的形式返回,使用英語的地區設置}
方法三:調用庫函數
1.首先聲明了一個私有的靜態變量calendar
,并調用其靜態方法getInstance()
獲取一個默認時區的Calendar
實例。然后聲明了一個私有的靜態變量weekFormat
,它是一個SimpleDateFormat
對象,用于格式化星期幾信息。
2.使用set(year, month - 1, day)
方法將calendar
設置為給定日期。由于Calendar
中的月份從0開始,所以需要將傳入的month
減去1
3.使用weekFormat.format(calendar.getTime())
方法將calendar
中的時間格式化為星期幾的字符串,其中weekFormat
是一個SimpleDateFormat
對象,通過設置格式為"EEEE"來表示星期幾的全名形式
private static final Calendar calendar = Calendar.getInstance();
//獲取一個默認時區的`Calendar`實例private static final SimpleDateFormat weekFormat = new SimpleDateFormat("EEEE");
//格式化星期幾信息public String dayOfTheWeek3(int day, int month, int year) {calendar.set(year, month - 1, day);// 將`calendar`設置為給定日期return weekFormat.format(calendar.getTime());//`calendar`中的時間格式化為星期幾的字符串}
1154. 一年中的第幾天
方法一:直接計算
思路:
1.根據所給的字符串,分別截取出年月日的信息,轉化成整數
2.根據當前年份,判斷是否為閏年,是閏年:二月加1
3.之前月份天數之和,最后加上天數的信息
public int dayOfYear(String date) {int year = Integer.parseInt(date.substring(0, 4));int month = Integer.parseInt(date.substring(5, 7));int day = Integer.parseInt(date.substring(8));//調用substring分別截取出年月日的信息,轉化為整數int[] monthes = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//每個月份的天數if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {++monthes[1];}//如果當前的閏年,二月加1天(直接改變月份字典中的數)int ans = 0;for (int i = 0; i < month - 1; ++i) {ans += monthes[i];//每個月天數之和}return ans + day;}
方法二:調用庫函數
思路
1.指定日期字符串的格式
2.解析為 LocalDate 對象
3.返回該日期在其所屬年份中的天數
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//定義了一個 DateTimeFormatter 對象,它指定了日期字符串的格式為 "yyyy-MM-dd"public int dayOfYear2(String date) {return LocalDate.parse(date, formatter).getDayOfYear();//LocalDate.parse() 方法將日期字符串解析為 LocalDate 對象//getDayOfYear() 方法則返回該日期在其所屬年份中的天數}
點擊移步博客主頁,歡迎光臨~