java - 从 System.nanoTime() 生成日期

标签 java date

我正在编写用于实现秒表的代码。我用 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/

相关文章:

Java 日历时区

php - 从维度数组计算相同日期值

oracle - 我想转换此日期格式 1970-01-01T00 :00:00-08:00 to only dd/mm/yyyy in oracle. 但是通过使用下面的查询我在转换中遇到错误

java - 从批处理文件中发现 Java 安装在哪里?

java标题栏修改

java - 属性文件的访问和编码

date - matplotlib plot_date()在指定日期添加垂直线

ios - 从 NSString 获取月份,返回空值

java - 如何在jsp页面中以列表形式显示搜索结果?

c# - 什么时候应该使用弱引用?