How to measure the a time-span in seconds using System.currentTimeMillis()?

前端 未结 9 554
温柔的废话
温柔的废话 2020-12-02 10:09

How to convert System.currentTimeMillis(); to seconds?

long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)         


        
相关标签:
9条回答
  • 2020-12-02 10:30

    TimeUnit

    Use the TimeUnit enum built into Java 5 and later.

    long timeMillis = System.currentTimeMillis();
    long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
    
    0 讨论(0)
  • 2020-12-02 10:33
    TimeUnit.SECONDS.convert(start6, TimeUnit.MILLISECONDS);
    
    0 讨论(0)
  • 2020-12-02 10:35

    From your code it would appear that you are trying to measure how long a computation took (as opposed to trying to figure out what the current time is).

    In that case, you need to call currentTimeMillis before and after the computation, take the difference, and divide the result by 1000 to convert milliseconds to seconds.

    0 讨论(0)
提交回复
热议问题