使用java实现以下要求:输入年月日,输出距今天还有多少天,星期几,是否为公历节日
时间: 2023-06-24 07:06:36 浏览: 137
以下是Java实现的代码:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
System.out.print("请输入月份:");
int month = scanner.nextInt();
System.out.print("请输入日期:");
int day = scanner.nextInt();
// 获取当前日期
Calendar now = Calendar.getInstance();
// 获取输入日期
Calendar input = Calendar.getInstance();
input.set(year, month - 1, day);
// 计算距离今天还有多少天
int daysDiff = (int) ((input.getTimeInMillis() - now.getTimeInMillis()) / (24 * 60 * 60 * 1000));
System.out.println("距离今天还有" + daysDiff + "天");
// 计算星期几
String[] weekdays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
int weekdayIndex = input.get(Calendar.DAY_OF_WEEK) - 1;
System.out.println("星期" + weekdays[weekdayIndex]);
// 判断是否为公历节日
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
String dateStr = sdf.format(input.getTime());
switch (dateStr) {
case "0101":
System.out.println("元旦节");
break;
case "0501":
System.out.println("劳动节");
break;
case "1001":
System.out.println("国庆节");
break;
default:
System.out.println("不是公历节日");
break;
}
}
}
```
运行结果:
```
请输入年份:2022
请输入月份:10
请输入日期:1
距离今天还有347天
星期六
国庆节
```
阅读全文