java倒計時三種簡單實現方式

來源:互聯網
上載者:User

標籤:技術分享   imei   trace   util   class   dex   實現   interrupt   pack   

寫完js倒計時,突然想用java實現倒計時,寫了三種實現方式

一:設定時間長度的倒計時;

二:設定時間戳記的倒計時;

三:使用java.util.Timer類實現的時間戳記倒計時

代碼如下:

 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 }

運行結果:

time1()結果:

time2()結果:

time3()結果:

java倒計時三種簡單實現方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.