aimer311 发表于 2013-2-5 01:17:29

Timer的问题,把他写下来,勉励自己

要实现的逻辑是每天的0点0分执行myTask()的内容。

<div style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; WIDTH: 95%; PADDING-TOP: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.text.SimpleDateFormat;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Calendar;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Date;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Timer;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublic class MyTimer
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   static Timer timer = new Timer();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif   public MyTimer()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif   ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      java.util.Date date = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      SimpleDateFormatsdf = new SimpleDateFormat ();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      try   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      ...{   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif         date = sdf.parse("2007-05-24 00:00:00");   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      catch (Exception e)   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif      ...{   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif         e.printStackTrace();   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif         System.out.println("Timer error!");   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif      }   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif      timer.schedule(new MyTask(),date,24*60*60*1000);//执行计划,每天执行一次
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif   }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: Timer的问题,把他写下来,勉励自己