Solved

how do i track time in java????

Posted on 2004-04-11
2
210 Views
Last Modified: 2010-05-18
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
Comment
Question by:jhson114
2 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10800238
long startTime = System.currentTimeMillis();

[your code that needs to be timed here]

long endTime = System.currentTimeMillis();

long elapsedTimeInMilliSeconds = endTime - startTime;
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 50 total points
ID: 10804157
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 37
add projects t working set in maven 2 37
printf performancy 11 83
Cisco ASA: Java web start no go, asdm launcher no go 3 56
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question