I wrote some code for a simple class that calculates a start time to a stop a time. It's nothing very difficult but for those of you who want a simple class to use for a stop watch program or a counter program or something or other you can use this snippet of code.. This will probably work with Java programs, but for my purpose, I just used it for Android.. What I did was create a class called stopwatch and inserted the code shown below. To start the stop watch just use stopwatch.startTime(); . To get the current time passed just use long times[] = stopwatch.getTimePassed(); which will return the seconds, minutes, and hours in an array [0], [1], [2].. To stop the timer just use long times[] = stopwatch.stopTime(); .. Their is also a way to get just theseconds that is in their. Juse use long seconds = stopwatch.getSeconds();.. Let me know what you think..
public class stopwatch {
//declares and initializes variables
static long start = 0;
static long stop = 0;
public static void startTime()
{
//gets the current system time in Nano seconds
start = System.nanoTime();
}
public static long[] stopTime()
{
stop = System.nanoTime();
//gets the difference between the start time and stop time and gets the seconds
long timeSecs = (stop - start)/1000000000;
long[] times = {0, 0, 0};
//times[0] is SECONDS. times[1] is MINUTES. times[2] is HOURS.
//times[1] is minutes. seconds / 60 = minutes
times[1] = timeSecs / 60;
//times[0] is seconds. gets the remainder of 60
times[0] = timeSecs % 60;
//times[2] is hours. minutes / 60 = hours
times[2] = times[2] / 60;
//times[1] is minutes. gets the remainder of 60 minutes
times[1] = times[1] % 60;
return times;
}
public static long[] getTimePassed()
{
long newstop = System.nanoTime();
long timeSecs = (newstop - start)/1000000000;
long[] times = {0, 0, 0};
//times[1] is minutes. seconds / 60 = minutes
times[1] = timeSecs / 60;
//times[0] is seconds. gets the remainder of 60
times[0] = timeSecs % 60;
//times[2] is hours. minutes / 60 = hours
times[2] = times[2] / 60;
//times[1] is minutes. gets the remainder of 60 minutes
times[1] = times[1] % 60;
return times;
}
public static long getSeconds()
{
long newstop = System.nanoTime();
long seconds = (newstop - start)/1000000000;
return seconds;
}
}