功能需求:打印当前系统时间,模拟倒计时
发布日期:2021-05-12 23:59:41 浏览次数:16 分类:精选文章

本文共 1261 字,大约阅读时间需要 4 分钟。

模拟倒计时实现与打印系统当前时间

  • 模拟倒计时
  • 本代码模拟了一个倒计时的计数器功能。通过在控制台逐次打印数字,模拟了一个从10倒计到0的过程。

    import java.util.SimpleTimeZone;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class TestSleep {
    public static void main(String[] args) {
    try {
    tenDown();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    public static void tenDown() throws InterruptedException {
    int num = 10;
    while (true) {
    Thread.sleep(1000);
    System.out.println(num--);
    if (num <= 0) {
    break;
    }
    }
    }
    }
    1. 打印系统当前时间
    2. 本代码实现了一个持续打印系统当前时间的功能,每隔一秒输出一次当前时间。

      import java.text.SimpleDateFormat;
      import java.util.Date;
      public class TestSleep {
      public static void main(String[] args) {
      Date startTime = new Date(System.currentTimeMillis());
      while (true) {
      try {
      Thread.sleep(1000);
      SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
      System.out.println(formatter.format(startTime));
      startTime = new Date(System.currentTimeMillis());
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      }
      }
    上一篇:76. 双亲委派机制
    下一篇:75. 索引设计的原则

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月14日 09时56分32秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章