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