TimerDemo.java
Download TimerDemo.java
1: // Demo for Timers. This code prints out the current time every 5 seconds
2: // (and also plays a "beep" sound). "%Tr" means to format as "hh:mm:ss ?M"
3: // (?M means either AM or PM). This continues until the user types
4: // control+C.
5: //
6: // Written 4/2021 by Wayne Pollock, Tampa Florida USA
7:
8: import java.util.*;
9: import java.awt.Toolkit;
10: import java.time.LocalTime;
11:
12: public class TimerDemo {
13: public static void main (String[] args) {
14: Timer everyFiveSeconds = new Timer();
15:
16: TimerTask task = new TimerTask() {
17: @Override
18: public void run () {
19: System.out.printf("At the tone, the time will be %Tr%n",
20: LocalTime.now() );
21: Toolkit.getDefaultToolkit().beep();
22: }
23: };
24:
25: everyFiveSeconds.scheduleAtFixedRate( task, 0, 5_000 );
26: }
27: }