简体   繁体   English

带日期和时间的计时器Java 8

[英]Timer with Date and Times Java 8

My question is simple: Is it possible to use date and times of Java 8 with the class Timer (java.util.Timer). 我的问题很简单:是否可以将Java 8的日期和时间与Timer类(java.util.Timer)一起使用。

 Timer timer = new Timer(); 
 timer.schedule(TimerTask task, Date time)

In that case this class use Date, is there any solution for LocalTime, LocalDate, LocalDateTime. 在这种情况下,此类使用Date,是否有LocalTime,LocalDate,LocalDateTime的解决方案。

No, It is not possible. 不,不可能。 Even more it is not recommended to use it (you can find more details here ). 甚至不建议您使用它(您可以在此处找到更多详细信息)。 Alternatively you can use ScheduledExecutorService , here is an example . 或者,您可以使用ScheduledExecutorService ,这是一个示例

java8 release is not affecting the java.util.Date class, that would be a terrible compatibility issue for the old implementations running over there, java8发行版不影响java.util.Date类,对于在那里运行的旧实现而言,这将是一个可怕的兼容性问题,

java8 instead is bringing new APIs for time manipulation 相反,java8带来了用于时间操纵的新API

so can we do this in java 8? 那么我们可以在Java 8中做到这一点吗?

Timer timer = new Timer(); 
timer.schedule(task, time);

yes, as soon as time is a java.util.Date 是的,时间是一个java.util.Date

can we schedule a Timer in java 8 with the new time api features like LocalDate and LocalTime ? 我们可以在Java 8中使用新的time api功能(如LocalDateLocalTime)来调度Timer吗?

No, not directly, but those classes have methods allowing you to soon or later construct your old date object eg 不,不是直接的,而是这几类方法允许你迟早构建您的旧日期对象如

java.util.Date d = new SimpleDateFormat("yyyy-MM-dd").parse(localDate.toString());

as JB Nizet posted here 正如JB Nizet这里发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM