0

My question is simple: Is it possible to use date and times of Java 8 with the class 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.

Braisly
  • 385
  • 3
  • 13
  • 3
    You should avoid Timer, and use a ScheduledExecutorService in the first place. But anyway, Date has a method from(Instant). – JB Nizet Mar 12 '17 at 14:05
  • Hmmm why should I avoid Timer? Thanks – Braisly Mar 12 '17 at 14:14
  • 4
    For several reasons detailed in *Java concurrency in practice*: single thread which can reduce accuracy, bad handling of runtime exceptions, lack of control. – JB Nizet Mar 12 '17 at 14:22

2 Answers2

1

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.

Community
  • 1
  • 1
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
1

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 instead is bringing new APIs for time manipulation

so can we do this in java 8?

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

yes, as soon as time is a java.util.Date

can we schedule a Timer in java 8 with the new time api features like LocalDate and LocalTime?

No, not directly, but those classes have methods allowing you to soon or later construct your old date object e.g.

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

as JB Nizet posted here

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • As mentioned in a comment in the question, the Date class has the method from that can be used to create a Date from an Instant. So this is better than using SimpleDateFormat to parse a string. – Constantino Cronemberger Jul 04 '17 at 17:35