Java原生实现定时器

public static void main(String[] args) {
//新加定时器
//这个方法schedule(TimerTask task, Date firstTime, long period)
//获得当天的日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();

//定义开始时间字符
String timeStr = "15:27:00";
timeStr = sdf.format(date)+" "+timeStr;

//获得当天的指定时间的date对象

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
date = sdf.parse(timeStr);
//判断今天的执行时间是否已经过去,如果过去则改为明天
if(date.getTime()

   date = new Date(date.getTime()+24*60*60*1000);
   
  }

}catch(Exception e){
e.printStackTrace();

}

Timer timer = new Timer();
//timer.schedule(new testTask(), date, 246060*1000);
timer.schedule(new testTask(),date,246060*1000);
while(true){
try{

int ch = System.in.read();
if(ch-'c'==0){
 timer.cancel();
}

}
catch(Exception e){

e.printStackTrace();

}

}

//new roomdhtranSys().commoneMethod();
//System.out.println("结束!");

}

package com;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

public class testTask extends TimerTask {

public void run(){
new roomdhtranSys().commoneMethod();

}
}

上一篇:前后端交互


下一篇:Spring Boot配置支持跨域访问