我正在用 Java 创建一个程序,我想每分钟检查一次当前时间,以及它是否等于特定时间,例如 10am println("It is 10 am do this")。到目前为止,我得到的是这样的当前时间
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);
}
但是我怎样才能让它每分钟检查一次当前时间,如果可能的话同时检查一次。谢谢
最佳答案
您可以使用 ScheduledExecutorService :
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable checkTime = new Runnable() {
public void run() {
remindMe();
}
};
scheduler.scheduleAtFixedRate(checkTime, 0, 1, TimeUnit.MINUTES);
注意:Java 中的约定是方法名以小写开头。
关于java - 同时查看当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15298811/