I'm working on a school project in Java and need to figure out how to create a timer. The timer I'm trying to build is supposed to count down from 60 seconds.
Asked
Active
Viewed 2.1k times
2
-
2Console? GUI? What code do you have so far? – Borgleader Sep 17 '12 at 18:30
-
Please edit your question to include an [sscce](http://sscce.org/) that shows what you've tried; this related [example](http://stackoverflow.com/a/12451673/230513) may be a useful starting point. – trashgod Sep 17 '12 at 18:45
5 Answers
2
You can use:
int i = 60;
while (i>0){
System.out.println("Remaining: "i+" seconds");
try {
i--;
Thread.sleep(1000L); // 1000L = 1000ms = 1 second
}
catch (InterruptedException e) {
//I don't think you need to do anything for your particular problem
}
}
Or something like that
EDIT, i Know this is not the best option, otherwise you should create a new class:
Correct way to do this:
public class MyTimer implements java.lang.Runnable{
@Override
public void run() {
this.runTimer();
}
public void runTimer(){
int i = 60;
while (i>0){
System.out.println("Remaining: "+i+" seconds");
try {
i--;
Thread.sleep(1000L); // 1000L = 1000ms = 1 second
}
catch (InterruptedException e) {
//I don't think you need to do anything for your particular problem
}
}
}
}
Then you do in your code: Thread thread = new Thread(MyTimer);

F. Mayoral
- 175
- 1
- 10
-
I tried your method, and it worked, except that i "paused" every thing else in the program until it was done. Do you know how to fix that? – Johannes Flood Sep 17 '12 at 19:33
-
In order to do so, you would need to run this method in a new Thread, but we don't want to make things too complicated, i added new code in the original answer. – F. Mayoral Sep 17 '12 at 19:36
-
It still pauses, when i press the start button the button don't even go up until the timer is done! – Johannes Flood Sep 17 '12 at 19:43
-
added the correct way, or at least how i would do this, you need to create a new class for your timer, it has to implements Runnable, and the run method, i separated the timer function, and it's called when this new class is invoked when you call new Thread(MyTimer);, check it and let me know if it worked ;) – F. Mayoral Sep 17 '12 at 19:49
-
Still doesn't work. Here's how it looks right know: http://piclair.com/351ak and http://piclair.com/14tw4 – Johannes Flood Sep 17 '12 at 20:01
-
odd, are you importing the class? like, "import [location]MyTimer;"? (be sure to replace [location] with the package name where "MyTimer" is located, it may be the default package so you should do "import MyTimer;" in that case), check the little error icon at the left of the screen to see a hint – F. Mayoral Sep 17 '12 at 22:06
1
Since you didn't provide specifics, this would work if you don't need it to be perfectly accurate.
for (int seconds=60 ; seconds-- ; seconds >= 0)
{
System.out.println(seconds);
Thread.sleep(1000);
}

Jon7
- 7,165
- 2
- 33
- 39
-
For those of you that got 'Not a statement' Error, re-arrange the code to: `for (int seconds = 60; seconds >= 0; seconds--)` – Dec 01 '22 at 12:34
1
Look into Timer, ActionListener, Thread

Piyush Mattoo
- 15,454
- 6
- 47
- 56
-
With a bad question like this, I find this so-so answer to be the best general answer. A simple example use would be nice. – Howie Sep 03 '13 at 18:50
0
There are many ways to do this. Consider using a sleep function and have it sleep 1 second between each iteration and display the seconds left.

BSull
- 329
- 2
- 10
0
It is simple to countdown with Java. Lets say you want to countdown 10 min so Try this.
int second=60,minute=10;
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
second--;
// put second and minute where you want, or print..
if (second<0) {
second=59;
minute--; // countdown one minute.
if (minute<0) {
minute=9;
}
}
}
};
new Timer(delay, taskPerformer).start();

Farukest
- 1,498
- 21
- 39