import java.util.Calendar;
public class TestCalendar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar cl = Calendar.getInstance(); ----》实际上是创建了一个GregorianCalendar子类
System.out.println(cl.get(Calendar.YEAR)+"年"+cl.get(cl.MONTH) + "月" +
cl.get(cl.DAY_OF_MONTH) + "日" + cl.get(cl.HOUR) + ":"+
cl.get(cl.MINUTE) + ":" + cl.get(cl.SECOND));
System.out.println(cl.get(Calendar.YEAR)+"年"+cl.get(cl.MONTH) + "月" +
cl.get(cl.DAY_OF_MONTH) + "日" + cl.get(cl.HOUR) + ":"+
cl.get(cl.MINUTE) + ":" + cl.get(cl.SECOND));
}
}
Date类 (Calendar类几乎取代了Date类)
java.text.DateFormat与java.text.SimpleDateFormat子类
DateFormat是一个抽象类,
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestCalendar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar cl = Calendar.getInstance();
System.out.println(cl.get(Calendar.YEAR)+"年"+cl.get(cl.MONTH) + "月" +
cl.get(cl.DAY_OF_MONTH) + "日" + cl.get(cl.HOUR) + ":"+
cl.get(cl.MINUTE) + ":" + cl.get(cl.SECOND));
System.out.println(cl.get(Calendar.YEAR)+"年"+cl.get(cl.MONTH) + "月" +
cl.get(cl.DAY_OF_MONTH) + "日" + cl.get(cl.HOUR) + ":"+
cl.get(cl.MINUTE) + ":" + cl.get(cl.SECOND));
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
Date d;
try {
d = sdf1.parse("2003-03-15");
System.out.println(sdf2.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
new Timer().schedule(new TimerTask() //new Timer()启动的线程默认是非damon,只要非Damon的线程没有结束,则程序就不会结束 ,若new Timer(true),启动的线程是damon线程,当非damon线程结束后,damon线程也会消失
{
public void run()
{
Runtime.getRuntime().exec("calc.exe");
}
}
,30000); ---->隔30s后打开计算器
第二种:
new Timer().schedule(new TimerTask()
{
public void run()
{
Runtime.getRuntime().exec("calc.exe");
//添加结束任务线程的代码
// Timer.cancel();
// TimerTask.cancel(); this.cancel(); --->以后所有的任务都会取消,30S之前调用cancel函数,间隔时间调用的任务都会被取消
}
}
,30000); ---->隔30s后打开计算器
第三种:
class MyTimerTask extends TimeTask
{
private Timer tm = null;
public MyTimerTask(Timer tm)
{
this.tm = tm;
}
public void run()
{
Runtime.getRuntime().exec("calc.exe");
tm.cancel();
}
}
Timer tm = new Timer();
tm.schedule(new MyTimerTask(tm),30000); //tm调用schedule的时候会调用一个线程,线程在指定的时间后会
运行MyTimerTask中的run方法 等同于 new Thread(new MyTimerTask()).start(); 当线程启动时就会调用MyTimerTask中的run方法