如何从日期获取微秒? [英] How to get the microseconds from a Date?
问题描述
我试图从日期
中获取微秒,但我不能。
I am trying to get microseconds from a Date
, but I can't.
Date date = new Date()
推荐答案
否,日期
仅将值存储为毫秒精度。如果您希望获得微秒级的精度,则可能需要查看 JSR-310 或 java.sql.Timestamp 有点黑客-但不要指望任何内置类,例如 Calendar
和 DateFormat
No, Date
only stores values to millisecond accuracy. If you want microsecond accuracy, you might want to look at JSR-310, or java.sql.Timestamp as a bit of a hack - but don't expect any of the built-in classes such as Calendar
and DateFormat
to handle anything beyond milliseconds.
如果要尝试执行 timing (例如用于基准测试),则应使用 System.nanoTime
,但是应该只将它们用于类似秒表的用例-它不会为您提供有意义的当前挂钟时间方式。
If you're trying to perform timing (e.g. for benchmarking) you should use System.nanoTime
, but that should only be used for stopwatch-like use cases - it doesn't give you the "current wallclock time" in a meaningful way.
这篇关于如何从日期获取微秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!