用java实现输入一个日期计算是一年的第几天
时间: 2023-04-05 16:00:47 浏览: 130
您好,可以使用以下代码实现输入一个日期计算是一年的第几天:
```
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(0, 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};
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
daysOfMonth[1] = 29;
}
int sum = 0;
for(int i = 0; i < month - 1; i++){
sum += daysOfMonth[i];
}
sum += day;
System.out.println("这是" + year + "年的第" + sum + "天");
}
}
```
希望能帮到您!
阅读全文