import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class DynamicNewYearCountdown {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
int currentYear = now.getYear();
LocalDateTime newYear = LocalDateTime.of(currentYear + 1, 1, 1, 0, 0, 0);while (now.isBefore(newYear)) {
now = LocalDateTime.now();
long secondsRemaining = now.until(newYear, ChronoUnit.SECONDS);
long hours = secondsRemaining / 3600;
long minutes = (secondsRemaining % 3600) / 60;
long seconds = secondsRemaining % 60;
System.out.println("距离新年还有:" + hours + "小时 " + minutes + "分钟 " + seconds + "秒");
try {
Thread.sleep(1000); // 每隔1秒更新一次倒计时
} catch (InterruptedException e) {
e.printStackTrace();
}
}System.out.println("新年快乐!");
}
}
注意:这个示例假设新年始终在每年的1月1日。您可以根据需要进行修改和扩展,例如考虑闰年、特定时区等