如何在Java中的特定时间调用方法?

如何在Java中的特定时间调用方法?,第1张

如何在Java中的特定时间调用方法?

使用java.util.Timer类,您可以创建计时器并将其计划为在特定时间运行。

下面是示例:

//The task which you want to executeprivate static class MyTimetask extends TimerTask{    public void run()    {        //write your pre here    }}public static void main(String[] args) {    //the Date and time at which you want to execute    DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    Date date = dateFormatter .parse("2012-07-06 13:05:45");    //Now create the time and schedule it    Timer timer = new Timer();    //Use this if you want to execute it once    timer.schedule(new MyTimetask(), date);    //Use this if you want to execute it repeatedly    //int period = 10000;//10secs    //timer.schedule(new MyTimetask(), date, period );}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5016702.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-15
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存