【发布时间】:2020-03-31 07:25:19
【问题描述】:
我了解在 Java 中,可以使用 ScheduledExecutorService 在一定延迟后执行特定任务。 This 帖子显示了如何在特定日期执行任务,但不是 SimpleDateFormat 。例如,我有一个如下初始化的格式:
dateFormatter = new SimpleDateFormat("MM-dd-yyyy hh:mm aa");
另外,我想执行在 ActionListener 中的 else if 语句下声明的特定任务,如下所示:
foo.addActionListener(e -> {
if (condition) {
// some task
} else if (some other condition) { // what I want to be executed at particular date
// some other task
} else {
// another task
}
});
我如何初始化在某个日期在else if 语句内和/或使用else if 语句执行的ScheduledExecutorService,最好使用SimpleDateFormat?
【问题讨论】:
-
我建议你不要使用
SimpleDateFormat。这个类是出了名的麻烦和过时。而是使用DateTimeFormatter和java.time, the modern Java date and time API 中的其他类。 -
您的意思是,在
else部分中,您想安排某件事在某个日期和时间发生吗?抱歉,这个不清楚。另外,在哪个时区? -
还有你把搜索引擎放在哪里了? :-) 我相信它可能会带来很多有用的东西。
标签: java date simpledateformat scheduledexecutorservice