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 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 Demo countdown * */12 public class Timetest {All public static int time = $ * 60;14 Public St atic Calendar c;15 public static long endtime;16 public static Date date;17 public static long starttime;18 public static long midtime;19 public static void Main (string[] args) {22 c = calendar.getinstance (); C.set (2017, 4, 17, 0, 0, 0);//Note the setting of the month, 0-11 means January-December//C.set (Calendar.year), and//C.set (Calen Dar. MONTH, 4); +//C.set (Calendar.day_of_month, +);//C.set (calendar.hour_of_day, 0);//C.set ( Calendar.minute, 0);//C.set (Calendar.second, 0); endTime = C.gettimeinmillis (); date = new Date (); startTime = Date.gettime (); midtime = (endtime-starttime)/1000;33//time1 (); /Way 135 Time2 ();//mode 236//Time3 ();//mode 337}38 39/**40 * Way Three: Use Java.util.Timer class to Countdown to */42 p rivate static void Time3 () {45 Timer timer = new timer (); Timer.schedule (new TimerTask () public void Run () {midtime--;47 long hh = midtime/60/60% 60;48 lo ng mm = midtime/60% 60;49 long ss = midtime% 60;50 System.out.println ("still left" + hh + "Hour" + mm + "min" + SS + "SEC"); 51}52}, 0, 1000); 53}54 55/**56 * Mode two: set timestamp, Countdown 57 */58 private static void Time2 () {midtime > 0) {midtime--;62 long hh = m Idtime/60/60% 60;63 long mm = midtime/60% 60;64 long ss = midtime% 60;65 Syst Em.out.println ("Still remaining" + hh + "hours" + mm + "min" + SS + "SEC"); try {thread.sleep (1000); 68 69 } catch (InterRuptedexception e) {e.printstacktrace (); 71}72}73}74 75/**76 * Way One: Give the timer long countdown */78 private static void Time1 () {time > 0) {time--;81 try {thread.sleep (+); time/60/60 int hh = 60;84 int mm = Ti Me/60% 60;85 int ss = time% 60;86 System.out.println ("Still remaining" + hh + "Hour" + mm + "min" + SS + "seconds"); catch (Interruptedexception e) {e.printstacktrace (); 89}90 }91 92}93}
Operation Result:
TIME1 () Results:
Time2 () Results:
Time3 () Results:
Tags: java, java Countdown, Timer, Countdown
Java countdown Three simple ways to implement