Solved

how do i track time in java????

Posted on 2004-04-11
2
206 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
Increment alphanumeric sequence 6 81
pairstar challenge 2 42
endX challenge 2 50
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now