Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

how do i track time in java????

say for example... i'm making a car simulation, and i want the car to update its speed, gas, mileage, etc. after every seconds. how would i do that?? i need to be able to track time? i'm pretty new to this kind of thing. sorry if this is a stupid question.
0
jhson114
Asked:
jhson114
1 Solution
 
Tommy BraasCommented:
long startTime = System.currentTimeMillis();

[your code that needs to be timed here]

long endTime = System.currentTimeMillis();

long elapsedTimeInMilliSeconds = endTime - startTime;
0
 
mmuruganandamCommented:
You can't really keep track of each and every second directly in Java.

But you can use the threads to implement the same.

public class TimerThread extends Thread
{
   public TimerThread()
   {
      start();
   }

   public void run()
   {
      while (true)
      {
            // execute your part....
           
            try
            {
                Thread.sleep(1000); // one second delay....
            }
            catch (Exception e)
            {
                  // ignore
            }
      }
   
   }
}


This code will run forever and execute and/or track your thing each and every second.


Regards,
Muruga
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now