0

I want to see if the user completed a certain task in one second. I know that there are several ways to get time elapsed, but I am wondering what the most accurate way would be for this scenario. I want to get time elapsed ever since a certain time. For example, register a timer, and refer back to it later:

  1. User does something

  2. I check if user completed task in the one second timer

  3. User does something again

  4. I check if the user completed the task within the same one second timer that was running before. If not, make timer null.

  5. User does something again

  6. I check if user completed the task within one second timer. If not, make timer null.

I want to basically have one timer that runs for one second, as the user completes various tasks. After every task, I check whether or not the timer is still going. I don't want to use countdowntimer for this, as it is not accurate enough for one second. What method can I use for this?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

2 Answers2

0

Use System.currentTimeMillis() or System.nanoTime(), if you need a nanosecond precision. I guess, System.currentTimeMillis() precision should be enough for your case.

Record a timestamp just before starting a task, then just after completing a task and task a difference.

Check this answer for explanation which method is better suits for you.

Community
  • 1
  • 1
Konstantin Pavlov
  • 956
  • 1
  • 10
  • 24
  • I have tried that, but my problem is that I am doing all of this in a listener which is in a separate class. Whenever it starts listening again, my timestamp variable that I had recorded earlier gets erased, and I get null when I try to use it. How do I prevent this variable (double) from losing its data? – Ruchir Baronia Feb 09 '16 at 05:25
0

Use System.nanoTime() if you want the highest precission. System.currentTimeMillis() should work for you too though.

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

Just implement a simple timer using either of these methods.

private static final class Timer {

    private long start;
    private long end;

    static double toSeconds(long nanos) {
        return nanos / 1000000000.0;
    }

    void start() {
        end = -1;
        start = System.nanoTime();
    }

    double getCurrent() {
        return toSeconds(System.nanoTime() - start);
    }

    double stop() {
        end = System.nanoTime();
        return toSeconds(end - start); 
    }

    boolean isRunning() {
        return end == -1;
    }

}

public static void main(String[] args) {
    Timer timer = new Timer();

    timer.start();
    doSomething();
    if (timer.getCurrent() > 1.0) {
        double time = timer.stop();
        System.out.println("User completed task 1 in " + time);
    }

    doSomething();
    if (timer.isRunning() && timer.getCurrent() > 1.0) {
        double time = timer.stop();
        System.out.println("User completed task 1 & 2 in " + time);
    }

    doSomething();
    if (timer.isRunning() && timer.getCurrent() > 1.0) {
        double time = timer.stop();
        System.out.println("User completed task 1 & 2 & 3 in " + time);
    }

    System.out.println("All tasks finished");

}
MartinS
  • 2,759
  • 1
  • 15
  • 25
  • I have tried that, but my problem is that I am doing all of this time recording in a listener which is in a separate class. Whenever it starts listening again, my timestamp variable that I had recorded earlier gets erased, and I get null when I try to use it. How do I prevent this variable (double) from losing its data? – Ruchir Baronia Feb 09 '16 at 05:26
  • It's a bit difficult to say without knowing the code. Maybe you can check whether it's already recording the time? – MartinS Feb 09 '16 at 05:28
  • Depends on your code; maybe you can update the question? – MartinS Feb 09 '16 at 16:27