Java ActionListener中添加了日期格式的ScheduledExecutorService
我知道在Java中,可以使用Java ActionListener中添加了日期格式的ScheduledExecutorService,java,date,simpledateformat,scheduledexecutorservice,Java,Date,Simpledateformat,Scheduledexecutorservice,我知道在Java中,可以使用ScheduledExecutorService在一定延迟后执行特定任务。post显示如何在特定日期执行任务,但不使用SimpleDataFormat。例如,我有一个如下初始化的格式: dateFormatter = new SimpleDateFormat("MM-dd-yyyy hh:mm aa"); 此外,我想执行在ActionListener中的else if语句下声明的特定任务,例如: foo.addActionListener(e -> {
ScheduledExecutorService
在一定延迟后执行特定任务。post显示如何在特定日期执行任务,但不使用SimpleDataFormat
。例如,我有一个如下初始化的格式:
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
}
});
我如何初始化在某个日期,最好是使用SimpleDataFormat
,在内和/或使用else if
语句执行的ScheduledExecutorService
?java.time
务必使用java.time,现代java日期和时间API进行日期和时间工作。要取代旧的SimpleDateFormat
而使用的现代类是DateTimeFormatter
static DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MM-dd-yyyy hh:mm a", Locale.ENGLISH);
编辑:正如Basil Bourque在评论中所说,还可以在某个地方声明你的执行人服务,当你不再需要它时可以关闭它。例如:
static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
现在,在您的action listener(或您可能喜欢的任何其他地方)中,您可以执行以下操作:
} else {
String timeString = "04-01-2020 08:00 AM";
ZoneId zone = ZoneId.of("America/Curacao");
ZonedDateTime timeToExecute = LocalDateTime.parse(timeString, formatter)
.atZone(zone);
long delaySeconds = ChronoUnit.SECONDS.between(
ZonedDateTime.now(zone), timeToExecute);
executor.schedule(() -> System.out.println("It’s now " + timeString),
delaySeconds, TimeUnit.SECONDS);
}
记住通过调用来关闭执行器以释放资源
executor.shutdown();
或者,如果要取消已安排的任务,请改用shutdownNow()
我已经为所需的时间和时区设置了非常随机的值,所以请插入您的值。如果需要JVM的时区设置,可以选择ZoneId.systemDefault()
在许多其他优点中,java.time更好地支持计算从现在到所需计划时间的时间间隔长度。如果您需要比秒精度更好的精度,请使用毫秒、微秒甚至纳秒,它的方式非常类似(因为您的字符串似乎只有分钟精度,我认为没有必要这样做)
您提到的SimpleDateFormat
是一个臭名昭著的类麻烦制造者,您永远不会喜欢使用它。幸运的是,它也早已过时
链接
解释如何使用java.time。java.time
务必使用java.time,现代java日期和时间API进行日期和时间工作。要取代旧的SimpleDateFormat
而使用的现代类是DateTimeFormatter
static DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("MM-dd-yyyy hh:mm a", Locale.ENGLISH);
编辑:正如Basil Bourque在评论中所说,还可以在某个地方声明你的执行人服务,当你不再需要它时可以关闭它。例如:
static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
现在,在您的action listener(或您可能喜欢的任何其他地方)中,您可以执行以下操作:
} else {
String timeString = "04-01-2020 08:00 AM";
ZoneId zone = ZoneId.of("America/Curacao");
ZonedDateTime timeToExecute = LocalDateTime.parse(timeString, formatter)
.atZone(zone);
long delaySeconds = ChronoUnit.SECONDS.between(
ZonedDateTime.now(zone), timeToExecute);
executor.schedule(() -> System.out.println("It’s now " + timeString),
delaySeconds, TimeUnit.SECONDS);
}
记住通过调用来关闭执行器以释放资源
executor.shutdown();
或者,如果要取消已安排的任务,请改用shutdownNow()
我已经为所需的时间和时区设置了非常随机的值,所以请插入您的值。如果需要JVM的时区设置,可以选择ZoneId.systemDefault()
在许多其他优点中,java.time更好地支持计算从现在到所需计划时间的时间间隔长度。如果您需要比秒精度更好的精度,请使用毫秒、微秒甚至纳秒,它的方式非常类似(因为您的字符串似乎只有分钟精度,我认为没有必要这样做)
您提到的SimpleDateFormat
是一个臭名昭著的类麻烦制造者,您永远不会喜欢使用它。幸运的是,它也早已过时
链接
解释如何使用java.time。更早地建立executor服务
如何初始化在和/或使用else if执行的
你没有
不要在需要时初始化executor服务
您可以在更早的时候在别处实例化,在命名变量中保留引用。稍后根据需要调用该executor服务对象
可以调用executor服务在应用程序的其他部分运行其他类型的任务。executor服务不需要仅绑定到一种任务
重要您必须保留对该执行器服务的引用,以便它的后台线程池可以在某个时候正常关闭。否则,线程池可能会在其原始应用程序结束后继续运行
一般来说,我建议采用这种方法
- 当应用程序启动时,建立您的executor服务。保留一个引用,以便以后通过选择全局变量访问。也许是a,或a,或诸如此类
- 在应用程序运行期间,找到现有的executor服务。提交要运行的任务
- 当应用程序结束时,找到现有的executor服务。调用其关闭方法以结束其备份线程池
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor() ;
// In the hook for app shut-down.
ScheduledExecutorService scheduledExecutorService = Objects.requireNonNull( … ) ; // Locate the existing scheduled executor service.
scheduledExecutorService.shutdown() ;
将ses
引用存储在应用程序中的某个位置
计算等待的时间量,如中所示
当您的应用程序退出时,请关闭计划的executor服务
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor() ;
// In the hook for app shut-down.
ScheduledExecutorService scheduledExecutorService = Objects.requireNonNull( … ) ; // Locate the existing scheduled executor service.
scheduledExecutorService.shutdown() ;
尽早建立executor服务
如何初始化在和/或使用else if执行的
你没有
不要在需要时初始化executor服务
您可以在更早的时候在别处实例化,在命名变量中保留引用。稍后根据需要调用该executor服务对象
可以调用executor服务在应用程序的其他部分运行其他类型的任务。executor服务不需要仅绑定到一种任务
重要您必须保留对该执行器服务的引用,以便它的后台线程池可以在某个时间正常关闭