-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarm.java
More file actions
35 lines (27 loc) · 995 Bytes
/
Copy pathalarm.java
File metadata and controls
35 lines (27 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Timer;
import java.util.TimerTask;
import java.time.LocalDateTime;
import java.time.Duration;
public class AlarmClock {
public static void main(String[] args) {
// Set your alarm time here
LocalDateTime alarmTime = LocalDateTime.of(2026, 4, 21, 18, 30);
LocalDateTime now = LocalDateTime.now();
long delay = Duration.between(now, alarmTime).toMillis();
if (delay < 0) {
System.out.println("Alarm time is in the past!");
return;
}
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("⏰ Alarm ringing!");
// Simple beep sound
java.awt.Toolkit.getDefaultToolkit().beep();
timer.cancel(); // stop timer after execution
}
}, delay);
System.out.println("Alarm set for: " + alarmTime);
}
}