• JavaDailyReports10_15


    2020-10-15

    16:12:16

    今天学习了如何实现倒计时控制程序的运行:


     

     

     1 package timer;
     2 
     3 import java.util.Calendar;
     4 import java.util.Date;
     5 import java.util.Timer;
     6 import java.util.TimerTask;
     7 
     8 /**
     9  * java演示倒计时
    10  *
    11  */
    12 public class TimeTest {
    13     public static int time = 60 * 60 * 60;
    14     public static Calendar c;
    15     public static long endTime;
    16     public static Date date;
    17     public static long startTime;
    18     public static long midTime;
    19 
    20     public static void main(String[] args) {
    21         c = Calendar.getInstance();
    22         c.set(2017, 4, 17, 0, 0, 0);// 注意月份的设置,0-11表示1-12月
    23         // c.set(Calendar.YEAR, 2017);
    24         // c.set(Calendar.MONTH, 4);
    25         // c.set(Calendar.DAY_OF_MONTH, 17);
    26         // c.set(Calendar.HOUR_OF_DAY, 0);
    27         // c.set(Calendar.MINUTE, 0);
    28         // c.set(Calendar.SECOND, 0);
    29         endTime = c.getTimeInMillis();
    30         date = new Date();
    31         startTime = date.getTime();
    32         midTime = (endTime - startTime) / 1000;
    33 
    34          time1();//方式一
    35         //time2();// 方式二
    36          //time3();//方式三
    37     }
    38 
    39     /**
    40      * 方式三: 使用java.util.Timer类进行倒计时
    41      */
    42     private static void time3() {
    43         Timer timer = new Timer();
    44         timer.schedule(new TimerTask() {
    45             public void run() {
    46                 midTime--;
    47                 long hh = midTime / 60 / 60 % 60;
    48                 long mm = midTime / 60 % 60;
    49                 long ss = midTime % 60;
    50                 System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒");
    51             }
    52         },   0, 1000);
    53     }
    54 
    55     /**
    56      * 方式二: 设定时间戳,倒计时
    57      */
    58     private static void time2() {
    59 
    60         while (midTime > 0) {
    61             midTime--;
    62             long hh = midTime / 60 / 60 % 60;
    63             long mm = midTime / 60 % 60;
    64             long ss = midTime % 60;
    65             System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒");
    66             try {
    67                 Thread.sleep(1000);
    68 
    69             } catch (InterruptedException e) {
    70                 e.printStackTrace();
    71             }
    72         }
    73     }
    74 
    75     /**
    76      * 方式一: 给定时长倒计时
    77      */
    78     private static void time1() {
    79         while (time > 0) {
    80             time--;
    81             try {
    82                 Thread.sleep(1000);
    83                 int hh = time / 60 / 60 % 60;
    84                 int mm = time / 60 % 60;
    85                 int ss = time % 60;
    86                 System.out.println("还剩" + hh + "小时" + mm + "分钟" + ss + "秒");
    87             } catch (InterruptedException e) {
    88                 e.printStackTrace();
    89             }
    90         }
    91 
    92     }
    93 }
  • 相关阅读:
    gdb调试libtool封装的可执行文件
    转载 Linux top命令详解
    VirtualBox使用物理硬盘建立磁盘
    【sqlServer】常用语句整理
    【c#基础】特殊集合
    【c# 基础】各种集合的不同情况下的性能
    【c#集合】Lookup 类 有序字典 SortedDictionary<TKey,TValue> 集
    【c#基础】集合--栈Stack 链表 LinkList 有序列表SortedList 字典Dictionary
    【c#基础】集合
    【c#基础】委托、Lambda表达式和事件
  • 原文地址:https://www.cnblogs.com/rainbow-1/p/13821738.html
Copyright © 2020-2023  润新知