|
1 package com.duduli.li.timertask;
2
3 import java.util.Date;
4 import java.util.Timer;
5
6 public class TimerTask {
7 public void timerTask(){
8
9
10 java.util.TimerTask task = new java.util.TimerTask() {
11 @Override
12 public void run() {
13 // TODO Auto-generated method stub
14 System.out.println("running");
15 }
16 };
17 Timer timer = new Timer();
18 //当前时间开始每一分钟执行一次timertask
19 timer.scheduleAtFixedRate(task, new Date(), 1000);
20 }
21
22 /**
23 * @param args
24 */
25 public static void main(String[] args) {
26 // TODO Auto-generated method stub
27 TimerTask tt = new TimerTask();
28 tt.timerTask();
29 }
30
31 }
32
1 package com.duduli.li.timertask;
2
3 import java.util.Date;
4 import java.util.Timer;
5
6 public class CheckTime {
7 public void timerTask(){
8 java.util.TimerTask task = new java.util.TimerTask() {
9 @Override
10 public void run() {
11 // TODO Auto-generated method stub
12 if(checkSecondTime()){
13 System.out.println("is zore");
14 }
15 }
16 };
17 Timer timer = new Timer();
18 timer.scheduleAtFixedRate(task, new Date(), 1000);
19 }
20
21 @SuppressWarnings("deprecation")
22 public boolean checkSecondTime(){
23 boolean boo = false;
24 Date date = new Date();
25 int i = date.getSeconds();
26 System.out.println(i);
27 if (0 == i){
28 boo = true;
29 }
30 return boo;
31 }
32 public static void main(String[] args) {
33 // TODO Auto-generated method stub
34 CheckTime ct = new CheckTime();
35 ct.timerTask();
36 }
37
38 }
39 |
|