题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strs = in.nextLine().split(" ");
        Integer year = Integer.parseInt(strs[0]);
        Integer month = Integer.parseInt(strs[1]);
        Integer day = Integer.parseInt(strs[2]);
	    // 方法1
        int[] monthDay = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	    // 满足闰年条件:
	    // 1、能被4整除 并且不能被100整除
	    // 2、能被400整除
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            monthDay[1] = 29;
        }

        int result = 0;
        for (Integer i = 0; i < month - 1; i++) {
            result += monthDay[i];
        }
        result += day;
        System.out.println(result);
	  
	  // 方法2
	  Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
    }
}

全部评论

相关推荐

03-06 20:09
贵州大学 Java
King987:你这个学历找个中大厂刷实习经历都是可以的,但是项目要有亮点才行,这个什么外卖就不要做了,去找找最新的项目,至少涉及高并发或者是新型的AI技术mcp rag啥的 ,我在出简历点评,但是你这个没什么好点评的,内容太少,而且含金量太低。自己改一改吧,或者看一下我的项目地址中,那里有大厂最近做过的实习项目
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务