java定时器Timer,每天00:00执行任务

版权声明:本人原创,转载需说明文章出处     https://blog.csdn.net/persistencegoing/article/details/89447634

https://blog.csdn.net/persistencegoing/article/details/84376427

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

public class TimerDemo {

    public static void main(String[] args) throws Exception {

        //得到时间类

        Calendar date = Calendar.getInstance();

        //设置时间为 xx-xx-xx 00:00:00

        date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 0, 0, 0);

        //一天的毫秒数

        long daySpan = 24 * 60 * 60 * 1000;

        //得到定时器实例

        Timer t = new Timer();

        //使用匿名内方式进行方法覆盖

        t.schedule(new TimerTask() {

            public void run() {

                //run中填写定时器主要执行的代码块

                System.out.println("定时器执行..");

                //算了,读取文件我也加上吧

                //你没说是文本还是文件,我都用字节流了。

                FileInputStream fis = new FileInputStream("D:\\a.txt");

                byte[] b = new byte[1024];

                int len = 0;

                while((len=fis.read(b))!=-1){

                    //读取输出呀呀呀呀......

                    System.out.println(new String(b,0,len));

                }

            }

        }, date.getTime(), daySpan); //daySpan是一天的毫秒数,也是执行间隔

    };

}

希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职     

群号:721 515 304

猜你喜欢

转载自blog.csdn.net/persistencegoing/article/details/89447634
今日推荐