forked from codednepal/hacktober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.java
37 lines (37 loc) · 883 Bytes
/
timer.java
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
36
37
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class ScheduleTimer
{
public static void main(String args[])
{
//instance of the Timer class
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
//represent the time after which the task will begin to execute
int i = 5;
@Override
public void run()
{
if(i>0)
{
System.out.println(i);
i--;
}
else
{
System.out.println("Wish You Very Happy Birthday!!");
//cancel the task once it is completed
timer.cancel();
}
}
};
//creating an instance of the Calendar class
Calendar date = Calendar.getInstance();
//setting the date and time on which timer will begin
date.set(2022, Calendar.MARCH, 30,23, 59, 54);
//enables the counter to count at a rate of 1 second
timer.scheduleAtFixedRate(task, date.getTime(), 1000);
}
}