您的位置:首页 > 编程语言 > Java开发

java定时器

2016-03-02 11:19 323 查看
1:Java 定时器的使用
public class TestTimer {

    static int count = 0;

    

    public static void showTimer() {

        TimerTask task = new TimerTask() {

            @Override

            public void run() {

                //需要执行的操作 

                System.out.println("时间=" + new Date() + " 执行了次"); 

            }

        };

        //设置执行时间

        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);

        int month = calendar.get(Calendar.MONTH);

        int day = calendar.get(Calendar.DAY_OF_MONTH);//每天

        //定制每天的21:09:00执行,

        calendar.set(year, month, day, 21, 9, 00);

        Date date = calendar.getTime();

        Timer timer = new Timer();

        System.out.println(date);

        

        int period = 2 * 1000;

        //每天的date时刻执行task,每隔2秒重复执行

        timer.schedule(task, date, period);

        //每天的date时刻执行task, 仅执行一次

        timer.schedule(task, date);

    }

    public static void main(String[] args) {

        showTimer();

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: