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

计算日期到天数转换

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));
    }
}

全部评论

相关推荐

07-07 11:33
江南大学 Java
已经在暑假实习了&nbsp;,没有明确说有hc,纠结实习到八月份会不会有点影响秋招毕竟感觉今年好多提前批
程序员小白条:92的话准备提前批,其他没必要,没面试机会的,而且你要准备充分,尤其八股和算法题
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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