所以我拥有的是我想要倒计时的小时分和秒数的 3 个变量.
So what I have is 3 variable of the hour minutes and seconds of the time i want to countdown towards.
我怎样才能让它计入我拥有的这个时间,它将输出 3 个包含剩余小时分和秒的变量.有人可以帮我吗?
how can I make it to count towards this time that I have and it will output 3 variables that contains the remaining hours minutes and seconds. can anyone help me?
推荐答案您可以使用 java.time.LocalTime 来创建开始和结束时间,并以指定的间隔循环遍历它们之间的所有时间例如以下代码以一秒的间隔从 start 循环到 end.
You can use java.time.LocalTime to create the start and end times and loop through all times between them with the specified interval e.g. the following code loops from the start until the end at an interval of one second.
import java.time.LocalTime; class Main { public static void main(String[] args) throws InterruptedException { LocalTime start = LocalTime.of(10, 20, 15); LocalTime end = LocalTime.of(10, 15, 20); // Count down every second from start until end for (LocalTime time = start; time.isAfter(end); time = time.minusSeconds(1)) { System.out.println(String.format("%02d:%02d:%02d", time.getHour(), time.getMinute(), time.getSecond())); Thread.sleep(1000); } } }输出:
10:20:15 10:20:14 10:20:13 ... ... ...在 Trail 中了解有关现代日期时间 API 的更多信息:日期时间.
Learn more about the modern date-time API at Trail: Date Time.
更多推荐
我试图在某个小时的第二天获得剩余时间
发布评论