Skip to main content
GameDev.net gamedev.net
🔒 Locked

Java Timer?

Started by Samsung Sep 15, 2009 at 4:37 PM 2 replies 650+ views
Original Post
Samsung
Samsung
Okay, so first of all thanks to everyone who answered my previous question - I've gotten that pretty much figured out now, thanks to the gamedev community :P So right now I'm just trying to make a very simple timer... I'm not even sure if I want to use the actual Timer class - all I want to do is set a certain value to false after 3 seconds This sounds simple enough but when I google for something like that I can't seem to find what I'm looking for. Can anyone explain it in a way that a complete noob at java would understand, or point me towards some nice tutorials for what I want? A million thanks, Sam
Antheus
Antheus
Quote:
Original post by Samsung

Can anyone explain it in a way that a complete noob at java would understand, or point me towards some nice tutorials for what I want?


Considering that putting "java timer" into google returns documentation as first link, and examples for everything else, is there something specific you need? Some special requirements regarding the timer?
StudioFortress
StudioFortress
You can get the current time, in milliseconds, by using the method: System.currentTimeMillis(). You can use this for checking how much time has passed.

A simple Timer class:
public class Timer    {        private final long end;                public Timer(long duration)        {            end = System.currentTimeMillis() + duration;        }                public boolean isPastDuration()        {            return end < System.currentTimeMillis();        }    }

and so to use for 3 seconds would be:
Timer timer = new Timer(3000); // 3 seconds in millisif (timer.isPastDuration()) {    // 3 seconds has passed}

You make a new timer with the duration given. It will then tell you if the duration has passed since the creation of the Timer. If you want it to automatically act after say 3 seconds then you'd be best off using a thread where it sleeps for the duration.
Samsung
Samsung
Thank you very much!
Exactly what I've been trying to do

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.