Packagecom.zx.ps.web.gzdb;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classCKx {//Initialize Count Static intCount = 0; Public Static voidShowtimer () {FinalTimer timer =NewTimer (); //Create a timer taskTimerTask task =NewTimerTask () {@Override Public voidrun () {++count; System.out.println ("Time =" +NewDate () + "performed" + Count + "Times");//1 plays Switch(count) { Case3: Timer.cancel (); System.out.println ("Scheduled time runs three times to end timed tasks!" "); Break; } } }; //Set Execution TimeCalendar Calendar =calendar.getinstance (); intYear =Calendar.get (calendar.year); intmonth =Calendar.get (Calendar.month); intDay = Calendar.get (calendar.day_of_month);//Daily//Customizing the Daily 11:40:00 execution,Calendar.set (year, month, day, 11, 58, 00); Date Date=Calendar.gettime (); intPeriod = 30 * 1000; //executes a task at the date of the day, repeating every 30 secondsTimer.schedule (task, date, period); //executes a task at the date of the day and executes only once//Timer.schedule (task, date); } Public Static voidMain (string[] args) {showtimer (); }}
Java Timer starts executing program