我试图在某个小时的第二天获得剩余时间

编程入门 行业动态 更新时间:2024-10-26 22:23:22
本文介绍了我试图在某个小时的第二天获得剩余时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

我试图在某个小时的第二天获得剩余时间

本文发布于:2023-11-24 02:56:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二天   剩余时间   小时   在某个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!