我试图在某个小时的第二天获得剩余时间 [英] I am trying to get the remaining time until the next day on a certain hour

查看:58
本文介绍了我试图在某个小时的第二天获得剩余时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我拥有的是我想要倒计时的小时分和秒数的 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.

这篇关于我试图在某个小时的第二天获得剩余时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆