Solved

how do i track time in java????

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

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

729 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