java 与日期和时间有关的类

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方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值