一简单例子,与时间及线程有关.
目的:在规定时间以后运行某程序.
import java.util.*;
public class TestCalendar {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
// TimeZone tz = TimeZone.getTimeZone("China");
//这里试了一下,还不知道如果想转换成本地时间该怎么来写.
Calendar cl = Calendar.getInstance(/*tz*/);
System.out.println(cl.get(Calendar.YEAR)+"年"+cl.get(Calendar.MONTH)+"月"+cl.get(cl.DAY_OF_MONTH)+"日"+cl.MINUTE+"分"+cl.SECOND+"秒");
//cl.add(cl.DAY_OF_YEAR,315);
//以下为通过设置某时间到达以后运行某程序.
class MyTimerTask extends TimerTask
{
private Timer tm = null;
public MyTimerTask(Timer tm){
this.tm = tm;
}
public void run()
{
try{
Runtime.getRuntime().exec("calc.exe");
}catch(Exception e){
e.printStackTrace();
}
//结束任务线程的代码
tm.cancel();
}
}
Timer tm = new Timer();
tm.schedule(new MyTimerTask(tm),3000);
}
}