Java countdown Three simple ways to implement

Source: Internet
Author: User

After writing the JS Countdown, suddenly want to use Java to achieve the countdown, wrote three ways to achieve

A: Set the countdown of the length;

Second: Set the time stamp countdown;

Three: Time stamp countdown using the Java.util.Timer class

The code is as follows:

1  Packagetimer;2 3 ImportJava.util.Calendar;4 Importjava.util.Date;5 ImportJava.util.Timer;6 ImportJava.util.TimerTask;7 8 /**9 * Java Demo CountdownTen  *  One  */ A  Public classTimetest { -      Public Static intTime = 60 * 60 * 60; -      Public StaticCalendar C; the      Public Static LongEndTime; -      Public Staticdate date; -      Public Static LongStartTime; -      Public Static LongMidtime; +  -      Public Static voidMain (string[] args) { +c =calendar.getinstance (); AC.set (2017, 4, 17, 0, 0, 0);//note the setting of the month, 0-11 means January-December at         //C.set (calendar.year); -         //C.set (Calendar.month, 4); -         //C.set (Calendar.day_of_month, n); -         //C.set (calendar.hour_of_day, 0); -         //C.set (calendar.minute, 0); -         //C.set (calendar.second, 0); inEndTime =C.gettimeinmillis (); -Date =NewDate (); toStartTime =date.gettime (); +Midtime = (endtime-starttime)/1000; -  the         //time1 ();//Way One *Time2 ();//Mode two $         //Time3 ();//Way ThreePanax Notoginseng     } -  the     /** + * Method Three: Use Java.util.Timer class to Countdown A      */ the     Private Static voidTime3 () { +Timer timer =NewTimer (); -Timer.schedule (NewTimerTask () { $              Public voidrun () { $midtime--; -                 LongHH = midtime/60/60% 60; -                 LongMM = midtime/60% 60; the                 LongSS = Midtime% 60; -System.out.println ("Still remaining" + hh + "Hour" + mm + "min" + SS + "SEC");Wuyi             } the}, 0, 1000); -     } Wu  -     /** About * Mode two: Set time stamp, Countdown $      */ -     Private Static voidtime2 () { -  -          while(Midtime > 0) { Amidtime--; +             LongHH = midtime/60/60% 60; the             LongMM = midtime/60% 60; -             LongSS = Midtime% 60; $System.out.println ("Still remaining" + hh + "Hour" + mm + "min" + SS + "SEC"); the             Try { theThread.Sleep (1000); the  the}Catch(interruptedexception e) { - e.printstacktrace (); in             } the         } the     } About  the     /** the * Way One: Give the timer a long countdown the      */ +     Private Static voidtime1 () { -          while(Time > 0) { thetime--;Bayi             Try { theThread.Sleep (1000); the                 intHH = time/60/60% 60; -                 intMM = time/60% 60; -                 intSS = Time% 60; theSystem.out.println ("Still remaining" + hh + "Hour" + mm + "min" + SS + "SEC"); the}Catch(interruptedexception e) { the e.printstacktrace (); the             } -         } the  the     } the}

Operation Result:

TIME1 () Results:

Time2 () Results:

Time3 () Results:

Java countdown Three simple ways to implement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.