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(&quot;yyyy-MM-dd HH:mm:ss&quot;);
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(&quot;2007-05-24 00:00:00&quot;);
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(&quot;Timer error!&quot;);
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]