Solved

how do i track time in java????

Posted on 2004-04-11
2
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Chrome and Firefox Java 5 88
java mysql insert application 14 68
Java Inheritance super keyword use 8 71
How does initial implicit objects  are set up in JSP? 7 40
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 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