Search code examples
javatimestampmillisecondsnanotime

Timestamp in milliseconds doesn't gets converted to hours, minutes and seconds properly?


Below is my simple timer class which is working fine in simplest cases. I am using below timer class to measure how much time certain code takes in getting executed.

public static class StopTimer {

    public static StopTimer getInstance() {
        return new StopTimer();
    }

    private long m_end = -1;
    private long m_interval = -1;
    private final long m_start;

    private StopTimer() {
        m_start = m_interval = currentTime();
    }

    public long getDuration() {
        long result = 0;

        final long startTime = m_start;
        final long endTime = isTimerRunning() ? currentTime() : m_end;

        result = nanoToMilliseconds(endTime - startTime);

        return result;
    }

    private long currentTime() {
        return System.nanoTime();
    }

    private long nanoToMilliseconds(final long nanoseconds) {
        return nanoseconds / 1000000L;
    }
}

Below is how I am using the above timer class. I get the timestamp in milliseconds it takes to execute some piece of code and then convert that timestamp tohours, minutes and seconds.

public static void main(String[] args) {

    StopTimer timer = StopTimer.getInstance();

    some_code_which_I_am_measuring();

    long timeTaken = timer.getDuration();

    int seconds = (int) (timeTaken / 1000) % 60;
    int minutes = (int) ((timeTaken / (1000 * 60)) % 60);
    int hours = (int) ((timeTaken / (1000 * 60 * 60)) % 24);    

    System.out.println("Total Time Taken: " + hours + " hours " + minutes + " minutes " + seconds + " seconds");

}

Problem Statement:-

The problem I am facing is timeTaken timestamp doesn't gets converted to hours, minutes and seconds properly. Meaning, if the code is running for total 2 minutes, then my sys.out shows correct result.

But suppose if this method some_code_which_I_am_measuring is running for two days, then my sys.out shows wrong result. As an example, I started my program on - Fri 5/23/2014 9:40 PM and it finished on Sun 5/25/2014 9:38 PM so my sys.out should tell me that it took ~48 hours something to execute the code. Right?

But in general it prints out -

Total Time Taken: 23 hours 57 minutes 35 seconds

which is wrong as you can see the date above it took 48 hours to complete the task.

Is there anything wrong in my above code?

Update:-

After adding days as well, overall timestamp should look like this?

int seconds = (int) (timeTaken / 1000) % 60;
int minutes = (int) ((timeTaken / (1000 * 60)) % 60);
int hours = (int) ((timeTaken / (1000 * 60 * 60)) % 24);
int days = (int) (timeTaken / (1000 * 60 * 60 * 24));

Solution

  • It doesn't make sense to take the number of hours and take the remainder when divided by 24, unless you further plan to list the number of days it took also.

    If hours is as far as you'll go with units, then take out the % 24:

    int hours = (int) (timeTaken / (1000 * 60 * 60));
    

    Then you'll see the correct output

    Total Time Taken: 47 hours 57 minutes 35 seconds
    

    Else, you may want to add a days calculation.

    int days = ((timeTaken / (1000 * 60 * 60 * 24));
    

    Then you can add that to the output, which would then show up as

    Total Time Taken: 1 day 23 hours 57 minutes 35 seconds