How to convert nanoseconds to seconds using the TimeUnit enum?

前端 未结 8 2084
小鲜肉
小鲜肉 2020-12-07 09:37

How to convert a value from nanoseconds to seconds?

Here\'s the code segment:

import java.io.*;
import java.util.concurrent.*; 
..

class Stamper          


        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 09:56

    This will convert a time to seconds in a double format, which is more precise than an integer value:

    double elapsedTimeInSeconds = TimeUnit.MILLISECONDS.convert(elapsedTime, TimeUnit.NANOSECONDS) / 1000.0;
    

提交回复
热议问题