输入周几,显示100天以后是周几?用Java代码表示
时间: 2024-03-03 11:49:31 浏览: 60
可以使用Java中的Calendar类来计算100天以后的日期,并获取对应的星期几。
以下是示例代码:
```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) {
// 获取输入的星期几
System.out.print("请输入星期几(例如:星期一):");
Scanner scanner = new Scanner(System.in);
String dayOfWeekStr = scanner.nextLine();
// 将星期几转换为Calendar中的常量
int dayOfWeek;
switch (dayOfWeekStr) {
case "星期日":
dayOfWeek = Calendar.SUNDAY;
break;
case "星期一":
dayOfWeek = Calendar.MONDAY;
break;
case "星期二":
dayOfWeek = Calendar.TUESDAY;
break;
case "星期三":
dayOfWeek = Calendar.WEDNESDAY;
break;
case "星期四":
dayOfWeek = Calendar.THURSDAY;
break;
case "星期五":
dayOfWeek = Calendar.FRIDAY;
break;
case "星期六":
dayOfWeek = Calendar.SATURDAY;
break;
default:
System.out.println("输入有误!");
return;
}
// 计算100天后的日期
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.add(Calendar.DATE, 100);
Date after100Days = calendar.getTime();
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String after100DaysStr = sdf.format(after100Days);
// 获取100天后是星期几
String[] weekdays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
int dayOfWeekAfter100Days = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// 输出结果
System.out.println("100天后是:" + after100DaysStr);
System.out.println("100天后是:" + weekdays[dayOfWeekAfter100Days]);
}
}
```
示例输出:
```
请输入星期几(例如:星期一):星期二
100天后是:2022年03月16日
100天后是:星期三
```
阅读全文