开发者

java实现固定时段倒计时的示例代码

开发者 https://www.devze.com 2025-08-19 10:28 出处:网络 作者: 爱码少年
目录一、简单需求二、代码实现三、运行结果四、知识扩展一、简单需求 某系统需要每隔一定时间给出时间倒计时提示,时间倒计时可能为每1、2、3、4、6、8、12或24小时,提示格式为HH:mm:ss
目录
  • 一、简单需求
  • 二、代码实现
  • 三、运行结果
  • 四、知识扩展

一、简单需求

某系统需要每隔一定时间给出时间倒计时提示,时间倒计时可能为每1、2、3、4、6、8、12或24小时,提示格式为HH:mm:ss

二、代码实现

import Java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Hours
{
    private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
    
    private static int[] hours = {1, 2, 3, 4, 6, 8, 12, 24};
    
    /**
     * 整点倒计时
     * 
     * @param args
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
    {
        // 3ms微调值
        long initDelay = 10000L - System.currentTimeMillis() % 10000L - 3;
        int hour = hours[RandomUtils.nextInt(0, hours.length)];
        executorService.scheduleAtFixedRate(() -> printTime(hour), initDelay, 10000, TimeUnit.MILLISECONDS);
    }
    
    private static void printTime(int hour)
    {
        // 定义时间间隔
        Long between = hour * 3600000L;
        long time = between - (System.currentTimeMillis() + 8 * 3600000L) % between;
        
        //去除100ms之内误差
        time += (100 - time % 100); 
        String after = DateFormatUtils.formatUTC(time, "HH':'mm':'ss.SSS");
        log.info("---------------  {} ==> {} H -------------------", after, hour);
    }
}

三、运行结果

2025-07-16 08:20:00.005 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:40:00.000 ==> 3 H -------------------

2025-07-16 08:20:10.001 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:39:50.000 ==> 3 H -------------------

2025-07-16 08:20:20.002 [pool-2-thread-2] INFO  c.f.s.Hours         &phpnbsp;          - ---------------  00:39:40.000 ==> 3 H -------------------

2025-07-16 08:20:30.002 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:39:30.000 ==> 3 H -------------------

2025-07编程客栈-16 08:20:40.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:39:20.000 ==> 3 H -------------------

2025-07-16 08:20:50.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:39:10.000 ==> 3 H -------------------

2025-07-16 08:21:00.006 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:39:00.000 ==> 3 H -------------------

2025-07-16 08:21:10.002 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:38:50.000 ==> 3 H -------------------

2025-07-16 08:21:20.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:38:40.000 ==> 3 H -------------------

2025-07-16 08:21:30.002 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:38:30.000 ==> 3 H -------------------

2025-07-16 08:21:40.002 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:38:20.000 ==> 3 H -------------------

2025-07-16 08:21:50.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:38:10.000 ==> 3 H -------------------

2025-07-16 08:22:00.005 [pool-2-编程客栈thread-1] INFO  c.f.s.Hours                    - ---------------  00:38:00.000 ==> 3 H -------------------

2025-07-16 08:22:10.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:37:50.000 ==> 3 H -------------------

2025-07-16 08:22:20.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:37:40.000 ==> android3 H -------------------

2025-07-16 08:22:30.002 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:37:30.000 ==> 3 H -------------------

2025-07-16 08:22:40.002 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:37:20.000 ==> 3 H -------------------

2025-07-16 08:22:50.002 [pool-2-thread-1] INFO  c.f.s.Hours                    - ---------------  00:37:10.000 ==> 3 H -------------------

2025-07-16 08:23:00.005 [pool-2-thread-2] INFO  c.f.s.Hours                    - ---------------  00:37:00.000 ==> 3 H -------------------

2025-07-16 08:23:10.002 [pool-2-thread-1] INFO  c.f.s.Hours                    编程客栈;- ---------------  00:36:50.000 ==> 3 H -------------------

四、知识扩展

java写一个从某个时间开始倒计时的程序

在日常生活中,我们经常会遇到需要进行倒计时的情况,比如倒计时比赛开始、倒计时活动结束等。通过编写一个简单的Java程序,我们可以实现一个从某个时间开始倒计时的功能。这篇文章将向大家介绍如何使用Java来实现这个倒计时程序,并提供代码示例。

实现原理

倒计时程序的实现原理其实很简单:我们需要获取当前时间和目标时间的差值,然后不断减少这个差值,直到倒计时结束。在Java中,我们可以使用java.util.Date或者java.time.LocalDateTime来表示时间,并通过计算两个时间的毫秒差值来实现倒计时。

代码示例

下面是一个简单的Java倒计时程序的实现,我们设置一个目标时间,然后每秒更新一次倒计时的剩余时间,直到倒计时结束。

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class CountdownTimer {
    public static void main(String[] args) {
        LocalDateTime targetTime = LocalDateTime.now().plusMinutes(1); // 设置目标时间为当前时间的1分钟后

        while (LocalDateTime.now().isBefore(targetTime)) {
            LocalDateTime currentTime = LocalDateTime.now();
            long secondsLeft = currentTime.until(targetTime, ChronoUnit.SECONDS);

            System.out.println("倒计时剩余时间:" + secondsLeft + "秒");

            try {
                Thread.sleep(1000); // 每隔1秒更新一次倒计时
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("倒计时结束!");
    }
}

运行结果

当我们运行上面的倒计时程序时,控制台将输出类似如下的内容:

倒计时剩余时间:59秒

倒计时剩余时间:58秒

倒计时剩余时间:57秒

...

倒计时结束!

到此这篇关于java实现固定时段倒计时的示例代码的文章就介绍到这了,更多相关java倒计时内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号