编写一段java程序1)设定考试时间,如“2023年6月17日09时00分00秒” (2)计算距离考试还有多少天多少小时多少分钟多少秒
时间: 2023-08-18 13:08:42 浏览: 35
好的,以下是一段 Java 代码,可以实现您的要求:
```java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class ExamCountdown {
public static void main(String[] args) {
// 设定考试时间
LocalDateTime examTime = LocalDateTime.of(2023, 6, 17, 9, 0, 0);
// 计算距离考试还有多少天、小时、分钟、秒
LocalDateTime now = LocalDateTime.now();
long days = now.until(examTime, ChronoUnit.DAYS);
long hours = now.until(examTime, ChronoUnit.HOURS) % 24;
long minutes = now.until(examTime, ChronoUnit.MINUTES) % 60;
long seconds = now.until(examTime, ChronoUnit.SECONDS) % 60;
// 输出结果
System.out.printf("距离考试还有 %d 天 %d 小时 %d 分钟 %d 秒", days, hours, minutes, seconds);
}
}
```
运行该程序,即可得到距离考试时间的天数、小时数、分钟数和秒数。请注意,以上计算结果也可能会因为不同的时区和具体时间而有所偏差。