题解 | #计算日期到天数转换#
计算日期到天数转换
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)); } }