1

我正在用java创建一个程序,我想每分钟检查一次当前时间,以及它是否等于特定时间,例如上午10点println(“现在是上午10点做这个”)。到目前为止,我得到了这样的当前时间

public static String RemindMe() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = new Date();
        System.out.println(dateFormat.format(date));

        if (dateFormat.format(date).equals("2013/03/08 11:19"))
            System.out.println("equal");
        else
            System.out.println("not equal");

        return dateFormat.format(date);
    }

但我怎样才能让它每分钟检查当前时间,如果可能的话同时检查。谢谢

4

1 回答 1

3

您可以使用ScheduledExecutorService

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable checkTime = new Runnable() {
    public void run() {
        remindMe();
    }
};
scheduler.scheduleAtFixedRate(checkTime, 0, 1, TimeUnit.MINUTES);

注意:Java 中的约定是以小写开头的方法名称。

于 2013-03-08T16:35:59.083 回答