java输入日期计算是这一年的第几天
时间: 2023-04-01 12:03:58 浏览: 182
您好,这个问题可以通过编写代码来实现。以下是一个示例代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入日期(yyyy-MM-dd): ");
String dateStr = scanner.nextLine();
int year = Integer.parseInt(dateStr.substring(, 4));
int month = Integer.parseInt(dateStr.substring(5, 7));
int day = Integer.parseInt(dateStr.substring(8, 10));
int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days = day;
for (int i = ; i < month - 1; i++) {
days += daysOfMonth[i];
}
if (month > 2 && isLeapYear(year)) {
days++;
}
System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + days + "天");
}
private static boolean isLeapYear(int year) {
return (year % 4 == && year % 100 != ) || year % 400 == ;
}
}
希望能帮到您!
阅读全文