与日期和时间有关的类
最常用几个类:Date、DateFormat和Calendar
Calendar类
-Calendar.add方法
-Calendar.get方法,获取年月日时间值
-Calendar.set方法
-Calendar.getInstance静态方法
-GregorianCalendar子类
Date类
java.text.DateFormat与java.text.SimpleDateFormat子类
Calendar cl = Calendar.getInstance();
System.out.println(cl.get(Calendar.YEAR) + "年" + cl.get(Calendar.MONTH) + "月" +
cl.get(Calendar.DAY_OF_MONTH) + "日" + cl.get(Calendar.HOUR) + ":" +
cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) );
cl.add(cl.DAY_OF_YEAR,315);//315天后的日期
System.out.println(cl.get(Calendar.YEAR) + "年" + cl.get(Calendar.MONTH) + "月" +
cl.get(Calendar.DAY_OF_MONTH) + "日" + cl.get(Calendar.HOUR) + ":" +
cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) );
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
try
{
Date d = sdf1.parse("2003-03-15");
System.out.println( sdf2.format(d) );
}
catch(Exception e)
{
e.printStackTrace();
}
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();
//this.cancel();
}
}
Timer tm = new Timer();
tm.schedule( new MyTimerTask(tm),30000);
Timer与TimerTask类
schedule方法主要有如下几种重载形式
schedule(TimerTask task,long delay);
schedule(TimerTask task,Date time);
schedule(TimerTask task,long delay,long period);
schedule(TimerTask task,Date firstTime,long period);
TimerTask类实现了Runnable接口,要执行的任务由它里面实现的
run方法来完成。