我正在编写用于实现秒表的代码。我用 System.nanoTime()
捕捉了一个瞬间。但我还想将该时刻转换并存储到日期字段中。当我尝试使用new Date(long msec)
时,它给了我一些荒谬的日期时间值。谁能帮我完成这个任务吗?
最佳答案
System.nanoTime
不是当前时间:
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.
这就是您遇到“一些荒谬的日期时间值”的原因。
使用System.currentTimeMillis
如果您想要以毫秒为单位捕获的日期(请参阅:unix time):
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
关于java - 从 System.nanoTime() 生成日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497029/