现在的位置: 首页 > 综合 > 正文

监控采集数据,整点定时器原理

2018年05月28日 ⁄ 综合 ⁄ 共 995字 ⁄ 字号 评论关闭
Java代码  收藏代码
  1. package test;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6. import java.util.Timer;  
  7. import java.util.TimerTask;  
  8.   
  9. public class Test {  
  10.   
  11.   
  12.     public static void main(String[] args) {  
  13.         final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  14.         Calendar c = Calendar.getInstance();  
  15.         // 整分钟已过秒  
  16.         int tmp1 = c.get(Calendar.SECOND);  
  17.         // 过5分钟的分钟  
  18.         int tmp2 = c.get(Calendar.MINUTE) % 5;  
  19.         // 存放到达5分钟整点秒  
  20.         int seconds = (5 - tmp2) * 60 - tmp1;  
  21.         // 调整启动时间  
  22.         c.add(Calendar.SECOND, seconds);  
  23.         System.out.println("定时器将在" + sdf1.format(c.getTime()) + "启动");  
  24.         // 启动定时器  
  25.         Timer timer1 = new Timer();  
  26.         TimerTask task1 = new TimerTask() {  
  27.             public void run() {  
  28.                 System.out.println(sdf1.format(new Date()));  
  29.             }  
  30.         };  
  31.         timer1.schedule(task1, c.getTime(), 300 * 1000);  
  32.   
  33.     }  
  34. }  

抱歉!评论已关闭.