如何从日期获取微秒? [英] How to get the microseconds from a Date?

查看:108
本文介绍了如何从日期获取微秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从日期中获取微秒,但我不能。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆